Is numel() supposed to accept more than one input argument?
Show older comments
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)
numel(A,1)
Accepted Answer
More Answers (1)
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], ':')
numel(A([2 3], :))
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
Hi Steven,
This call to numel invokes a method of class double.
A = magic(4);
which numel(A, [2 3], ':')
Any idea why class double (and probably others) doesn't follow the advice in the documentation to not do exactly this?
Categories
Find more on Customize Object Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!