ndims behavior in R2016a

2 visualizaciones (últimos 30 días)
Tony Pryse
Tony Pryse el 9 de Oct. de 2017
Editada: Walter Roberson el 9 de Oct. de 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

Respuesta aceptada

James Tursa
James Tursa el 9 de Oct. de 2017
Editada: James Tursa el 9 de Oct. de 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 comentario
Tony Pryse
Tony Pryse el 9 de Oct. de 2017
HA! How did I do that?? Oh, well ...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by