필터 지우기
필터 지우기

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

조회 수: 13 (최근 30일)
Matt J
Matt J 2021년 6월 24일
댓글: Paul 2024년 5월 24일
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

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 24일
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
Walter Roberson
Walter Roberson 2021년 6월 25일
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 2024년 5월 24일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 5월 24일
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
Paul
Paul 2024년 5월 24일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by