ndims behavior in R2016a

3 visualizzazioni (ultimi 30 giorni)
Tony Pryse
Tony Pryse il 9 Ott 2017
I'm running R2016a and don't understand the results I get with ndims. The doc says:
"N = ndims(A) returns the number of dimensions in the array A. The number of dimensions is always greater than or equal to 2."
and also:
"The number of dimensions in an array is the same as the length of the size vector of the array. In other words, ndims(A) = length(size(A))."
Trying a scalar, vector, matrix and 5-D array, I get:
>> ndims(1) ans = 3
>>length(size(1)) ans = 2
>> ndims(1:4) Index exceeds matrix dimensions.
>> length(size(1:4)) ans = 2
>> ndims(magic(3)) Index exceeds matrix dimensions.
>> length(size(magic(3))) ans = 2
and if I create a five-dimensional array using cat:
>>length(size(cat(5, [1 2; 4 5], [7 8; 3 2]))) ans = 5
but
>> ndims(cat(5, [1 2; 4 5], [7 8; 3 2]))
Index exceeds matrix dimensions.
Am I misusing ndims somehow? It couldn't be broken, could it??
Thanks

Risposta accettata

James Tursa
James Tursa il 9 Ott 2017
Modificato: James Tursa il 9 Ott 2017
You have likely inadvertently created a variable called "ndims" that is shadowing the MATLAB function of the same name. Clear that "ndims" variable and try things again. E.g., using R2016a:
>> ndims(1)
ans =
2
>> length(size(1))
ans =
2
>> ndims(1:4)
ans =
2
>> ndims(magic(3))
ans =
2
>> length(size(magic(3)))
ans =
2
>> length(size(cat(5, [1 2; 4 5], [7 8; 3 2])))
ans =
5
>> ndims(cat(5, [1 2; 4 5], [7 8; 3 2]))
ans =
5
Now, shadowing the MATLAB function "ndims" with a variable of the same name:
>> ndims = 3
ndims =
3
>> ndims(1)
ans =
3
>> ndims(1:4)
Index exceeds matrix dimensions.
>> ndims(magic(3))
Index exceeds matrix dimensions.
>> clear ndims
>> ndims(1)
ans =
2
>> ndims(1:4)
ans =
2
>> ndims(magic(3))
ans =
2

Più risposte (0)

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by