views:

109

answers:

3

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 array? Does it matter, or are they interchangeable in this case?

EDIT: Just for kicks:

alt text

Looks like they're the same performance-wise until you get to 100k elements.

+2  A: 

For a 1-by-N array, they are essentially the same. For a multidimensional array M, they can give different results:

  • numel(M) is equivalent to prod(size(M)).
  • length(M) is equivalent to max(size(M)).
gnovice
+4  A: 

In that case they return the same and there's no difference. In term of performance, it depends on the inner working of arrays in MATLAB. E.g. if there are metainformations about how many elements are in the array (no matter the shape), then numel is as fast as possible, while max(size(x)) seems to need more work to obtain the same thing (retrieving sizes, and then finding the max among those). I am used to use numel in that case, but performance speech (hypothetical) apart, I would say they are interchangeable.

ShinTakezou
You're right on the performance part. I just ran 100 iterations of numel vs length on x=1:100000000 and numel was on average 3.0919 times faster. Shouldn't matter much for smaller arrays though.
Doresoom
...and now I'm actually curious about the performance of numel/length vs size of array.
Doresoom
LENGTH will never be the performance bottleneck in your program! Only real performance improvements matter. For all other cases code readability matters.
Mikhail
@Mikhail it depends. First optimization is to write a better algorithm, but avoiding unuseful operations can help to make if faster anyway; of course, no reason to optimize any code that is executed once. (Thanks to Doresoom for testing, I made just a hypotesis about how things could be). I find `numel` readable as `length`, for 1xN arrays (indeed I find `numel` more expressive when usable, but it is subjective)
ShinTakezou
+2  A: 

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.

Mikhail
Good perspective - increased readability over a minute/insignificant increase in performance. +1
Doresoom
When I see NUMEL used, I don't assume it is being used on a matrix versus a vector, I simply assume that it is being used on an object for which the dimensions are unimportant and only the *number* of elements matters.
gnovice
It matters when you are going to refactor that code and do not know from the first glance whether it is a vector or matrix!
Mikhail
I would argue that it is the *variable name* that should indicate whether an object is a vector or matrix, not whether NUMEL or LENGTH is used on it.
gnovice
when the two are equivalent, I usually prefer NUMEL because its faster/easier to type, seriously!
Amro
@Amro: So is that laziness or efficiency? :)
Doresoom
what can I say, I'm that lazy ;) All kidding aside, Even though micro-optimization usually is not worth it, I believe @ShinTakezou has a valid point in that MATLAB's matrices internal structure probably stores the number of elements as meta-information; I came across an interesting but old newsgroup post which studied the `mxArray` structure found in `extern\include\matrix.h`: http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/c241d8821fb90275
Amro
Frankly, I would have said that a mathematician should prefer `numel`, since numeric array sounds like vector, and the length of a vector is something really different from "number of element" of the array(vector) (though, `norm` would be used instead, ... but even matlab doc has this remarks: «Note norm(x) is the Euclidean length of a vector x. On the other hand, MATLAB software uses "length" to denote the number of elements n in a vector») ...
ShinTakezou