ndims behavior in R2016a

2 次查看(过去 30 天)
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

采纳的回答

James Tursa
James Tursa 2017-10-9
编辑:James Tursa 2017-10-9
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by