Difference between MATLAB's numel and length functions

后端 未结 3 1840
有刺的猬
有刺的猬 2021-01-01 08:46

I know that length(x) returns max(size(x)) and numel(x) returns the total number of elements of x, but which is better for a 1 by n ar

3条回答
  •  星月不相逢
    2021-01-01 09:12

    As other said they are same for one-dimensional array.

    IMHO from code readability viewpoint length should be used on one-dimensional arrays. It is about "intentional programming", you see the code and understand what programmer had in mind when conceiving his work. So when I see numel I know it is used on a matrix.

    length vs. numel was a discussion topic in our team over a number of years. Ex senior developer did not cared about code reability, only about work being done and used only numel in otherwise not well readable/formatted code. Other guy is a matematician and used length only on numeric arrays being for him "real" arrays. For cell arrays and struct arrays he used numel.

提交回复
热议问题