Effacer les filtres
Effacer les filtres

ndims behavior in R2016a

4 vues (au cours des 30 derniers jours)
Tony Pryse
Tony Pryse le 9 Oct 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

Réponse acceptée

James Tursa
James Tursa le 9 Oct 2017
Modifié(e) : James Tursa le 9 Oct 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
  1 commentaire
Tony Pryse
Tony Pryse le 9 Oct 2017
HA! How did I do that?? Oh, well ...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by