Number of array dimensions
N = ndims(A)
example
N = ndims(A) returns the number of dimensions in the array A. The number of dimensions is always greater than or equal to 2. The function ignores trailing singleton dimensions, for which size(A,dim) = 1.
A
size(A,dim) = 1
collapse all
Create a row vector.
A = 1:5;
Find the number of dimensions in the vector.
ndims(A)
ans = 2
The result is 2 because the vector has a size of 1-by-5.
2
Create a cell array of character vectors.
A{1,1,1} = 'cell_1'; A{1,1,2} = 'cell_2'; A{1,1,3} = 'cell_3'
A = 1x1x3 cell array A(:,:,1) = {'cell_1'} A(:,:,2) = {'cell_2'} A(:,:,3) = {'cell_3'}
Find the number of dimensions of the cell array.
ans = 3
The result is 3 because the cell array has a size of 1-by-1-by-3.
3
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | timetable | cell | categorical | datetime | duration | calendarDuration
double
single
int8
int16
int32
int64
uint8
uint16
uint32
uint64
logical
char
string
struct
table
timetable
cell
categorical
datetime
duration
calendarDuration
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)).
ndims(A) = length(size(A))
This function fully supports tall arrays. For more information, see Tall Arrays.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
length | size
length
size
You have a modified version of this example. Do you want to open this example with your edits?