Clear Filters
Clear Filters

Is numel() supposed to accept more than one input argument?

38 views (last 30 days)
I accidentally did numel(A,1) instead of size(A,1) and didn't get an error message to alert me of the mistake. That's fine, I guess, as long as numel() is supposed to accept multiple input arguments for some reason. Is it?
A=rand(4,3);
numel(A)
ans = 12
numel(A,1)
ans = 1

Accepted Answer

Walter Roberson
Walter Roberson on 24 Jun 2021
This came up once before.
If I recall correctly, numel() is used internally in this way. When there is more than one parameter, the output is the product of the number of elements in the parameters after the first.
numel(-1,'all',rand(4,5))
ans = 60
Second parameter has 3 entries, third has 4*5 = 20, output is 3*4*5 = 60.
The datatypes of the additional parameters does not matter. That 'all' is not a keyword, just an array with 3 elements.
  3 Comments
Walter Roberson
Walter Roberson on 25 Jun 2021
I have to correct here.
The purpose is to calculate the output size of indexing the first parameter by the other parameters.
The difference compared to what I posted earlier is that ':' is recognized and will pull in the size of the corresponding dimension.
numel(rand(3,5,7),'all',rand(4,5),7)
ans = 60
numel(rand(3,5,7),'all',rand(4,5),':')
ans = 420
The ':' pulls in the size of the third dimension. But 'all' is just treated like a vector of 3 characters; it is not special.
Paul
Paul on 24 May 2024 at 19:36
I just ran into this (and posted a new question not knowing about this one). In my case, meant to preallocate an array
z = nan(numel(x),1);
but mistakenly typed
z = nan(numel(x,1));
The latter makes z a scalar and my code was running slower because of no preallocation.
I'm suprised the function accepts more arguments than is documented (at least as far back as 2019b) and gives the user no indication that something might be amiss. Is that the way Matlab is supposed to work?

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 24 May 2024 at 20:16
If I recall correctly this syntax used to be documented as a way for objects to be able to specify the size of the output that should be returned from indexing into them using expressions (that's why it accepts ':' and treats it as all the rows/columns/etc. in the appropriate dimension of the array.)
A = magic(4);
numel(A, [2 3], ':')
ans = 8
numel(A([2 3], :))
ans = 8
I believe we introduced the listLength and numArgumentsFromSubscript functions to handle this scenario without requiring authors of new classes to overload numel for their classes to handle these somewhat different use cases. [But classes that overloaded numel in the past should still work for backwards compatibility.] There's a note on the numArgumentsFromSubscript function documentation page that supports this: "Overload numArgumentsFromSubscript instead of numel to control the results from overloaded subsref and subsasgn. Overloading numArgumentsFromSubscript can avoid errors caused by overloading numel."
I'm not sure when we stopped documenting that numel could be called with more than 1 input argument. It's not documented on the reference page in release R2014a (ten year ago) but there is mention of this in the object-oriented programming documentation. I went back to the release when numel was introduced, release R12 (from 2000, not 2012; this is not R2012a or R2012b) and its help text had a note that:
NUMEL can also be used with SUBSREF to determine the
number of values that will be returned from a particular
call to SUBSREF.
I have not searched more thoroughly to determine in which specific release the help text and/or documentation changed.
  1 Comment
Paul
Paul on 24 May 2024 at 20:39
Hi Steven,
This call to numel invokes a method of class double.
A = magic(4);
which numel(A, [2 3], ':')
built-in (/MATLAB/toolbox/matlab/elmat/@double/numel) % double method
Any idea why class double (and probably others) doesn't follow the advice in the documentation to not do exactly this?

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!