Filter löschen
Filter löschen

ndims behavior in R2016a

4 Ansichten (letzte 30 Tage)
Tony Pryse
Tony Pryse am 9 Okt. 2017
Bearbeitet: Walter Roberson am 9 Okt. 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

Akzeptierte Antwort

James Tursa
James Tursa am 9 Okt. 2017
Bearbeitet: James Tursa am 9 Okt. 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 Kommentar
Tony Pryse
Tony Pryse am 9 Okt. 2017
HA! How did I do that?? Oh, well ...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by