AppNovice
AppNovice

Reputation: 21

Why is the number of dimensions of an array always greater than or equal to two in Matlab?

The documentation for ndims states that

N = ndims(A) returns the number of dimensions in the array A. The number of dimensions is always greater than or equal to 2

Doesn't a single dimension array, i.e., a row vector mean a dimension of 1?

Upvotes: 1

Views: 165

Answers (1)

Dev-iL
Dev-iL

Reputation: 24169

If you look inside ndims.m, you can see

Put simply, it is LENGTH(SIZE(X)).

Now size always returns a vector of length >=2, even for empty arrays (i.e. size([]) is [0 0]). Why that is? Likely a design choice made by TMW long ago.

If you want to measure "actual dimensions" you might want to use:

function nad = nActDims(in)

if numel(in) == 1
  nad = 1;
else
  nad = sum(size(in)>1);
end

or some combination of the functions isscalar, isvector, ismatrix.

Upvotes: 1

Related Questions