I don't find it counter-intuitive at all. In Mathematics there exist vectors of 1 dimension (even though they are isomorphic with scalars). Also, a matrix can perfectly have size 1x1.
It is true that a single number could be considered a scalar, a 1-vector or a 1x1 matrix. Matlab's view is:
- A scalar is considered to be a 1x1 matrix
- An n-vector is just a 1 x n or n x 1 matrix
More generally: trailing singleton dimensions don't count. For example, a 3D-array of size 2x3x4 can also be considered, say, a 5D-array of size 2x3x4x1x1. This works without error:
>> a = rand(2,3,4);
>> a(2,2,2)
ans =
0.2575
>> a(2,2,2,1,1)
ans =
0.2575
Now, if you want to check if A
is a vector, matrix, or multidimensional array with more than one element, use
numel(A)>1
The numel
function returns the number of elements of its input argument.