Is there a function that does the same thing as the numel function?

조회 수: 10 (최근 30일)
E
E 2022년 10월 21일
편집: John D'Errico 2022년 10월 21일
Hello, I was wondering what other functions could be used to achieve the same result as the numel function?

채택된 답변

Matt J
Matt J 2022년 10월 21일
편집: Matt J 2022년 10월 21일
Assuming X is one of the built-in Matlab variable types,
prod(size(X))
or
length(X(:))

추가 답변 (1개)

John D'Errico
John D'Errico 2022년 10월 21일
편집: John D'Errico 2022년 10월 21일
There are multiple ways you COULD achieve the same result as numel. Not all of them are good. For example, length ALMOST does the same thing, as long as you are working with a vector. But length is not the same if the argument is an array. So you could first reshape your array to be a vector, and THEN use length.
Or, you could use size. What would prod(size(X)) tell you about the array X?
For that matter, you could even use sum (in conjunction with some other tools.) Seriously, you could. You don't want to, as it is a crap solution, but it would work. Thus, what would this do:
sum(ones(size(X)),'all')
Heck, even nnz would work, as long as there are no NaNs in the array.
nnz(X | ~X)
Although that would also fail if your array is not numeric, a cell array, for example.
Just use numel.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by