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.

Answer from Dev-iL on Stack Overflow
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
ndims - Number of array dimensions - MATLAB
N = ndims(A) returns the number of dimensions in the array A. The number of dimensions is always greater than or equal to 2.
🌐
MathWorks
mathworks.com › system identification toolbox › model analysis › data extraction
ndims - Query number of dimensions of dynamic system model or model array - MATLAB
The number of dimensions is 2+p, where p is the number of array dimensions. In this example, p is 2 because sys is 3-by-1. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlab › external language interfaces › c with matlab › c matrix api
mxGetNumberOfDimensions (C) - Number of dimensions in mxArray - MATLAB
mxGetNumberOfDimensions returns the number of dimensions in the specified mxArray. The returned value is always 2 or greater. To determine how many elements are in each dimension, call mxGetDimensions. ... Pointer to an mxArray array, specified as const mxArray*. ... Run the command by entering ...
🌐
MathWorks
mathworks.com › simulink › block and blockset authoring › author block algorithms › author blocks using c/c++ › author blocks using c mex s-functions › configure c/c++ s-function features
ssGetBusElementNumDimensions - Get the number of dimensions for a bus element - MATLAB
static void mdlOutputs(SimStruct *S, int_T tid) { DTypeId dType = ssGetOutputPortDataType(S, 0); const void *u = ssGetInputPortSignal(S, 0); int numElems = ssGetNumBusElements(S, dType); int i; for(i=0; i<numElems; i++) { int_T numDims = ssGetBusElementNumDimensions(S, dType, i); const int_T *dims = ssGetBusElementDimensions(S, dType, i); int width = 1; int j; /* Compute width of the bus element */ for(j=0; j<numDims; j++) { width *= dims[j]; } } } ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
size - Array size - MATLAB
If A is a character vector of type char, then size returns the row vector [1 M] where M is the number of characters. However, if A is a string scalar, size returns [1 1] because it is a single element of a string array. For example, compare the output of size for a character vector and string: ... Dimension lengths, returned as a nonnegative integer scalar when dim is a positive integer scalar, a row vector of nonnegative integer scalars when dim is a vector of positive integers, or a 1-by-0 empty array when dim is an empty array.
🌐
MathWorks
mathworks.com › bioinformatics toolbox › microarray analysis › data import and management
ndims - Return number of dimensions in DataMatrix object - MATLAB
N = ndims(DMObj) returns the number of dimensions in DMObj, a DataMatrix object. The number of dimensions in a DataMatrix object is always 2. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 396577-confirm-the-number-of-dimensions
Confirm the number of dimensions - MATLAB Answers - MATLAB Central
April 22, 2018 - You are only ever going to have 2 dimensions with that code, because that is all that you have specified. In your representation, your are using separate columns of the data matrix to hold the x, y and z co-ordinates.
Find elsewhere
🌐
MathWorks
mathworks.com › matlab › external language interfaces › fortran with matlab › fortran matrix api › access fortran data
mxGetNumberOfDimensions (Fortran) - Number of dimensions in mxArray - MATLAB
mxGetNumberOfDimensions returns the number of dimensions in the specified mxArray. The returned value is always 2 or greater. To determine how many elements are in each dimension, call mxGetDimensions. ... Pointer to an mxArray array, specified as mwPointer. ... Run the command by entering ...
🌐
MathWorks
mathworks.com › matlab › external language interfaces › c with matlab › c matrix api
mxGetDimensions (C) - Pointer to dimensions array - MATLAB
Use mxGetDimensions to determine how many elements are in each dimension of the mxArray that pm points to. Call mxGetNumberOfDimensions to get the number of dimensions in the mxArray. ... Pointer to an mxArray array, specified as const mxArray*. ... Run the command by entering it in the MATLAB ...
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
length - Length of largest array dimension - MATLAB
L = length(X) returns the length of the largest array dimension in X. For vectors, the length is simply the number of elements. For arrays with more dimensions, the length is max(size(X)).
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1733985-how-many-dimensions-do-i-need
How many dimensions do I need? - MATLAB Answers - MATLAB Central
June 5, 2022 - Create a script to compute the number of feature dimensions N needed to represent at least 99.9% of the variance in the feature set of the humanactivity dataset using the 'pca' function. The steps...
🌐
Sc
people.math.sc.edu › howard › Classes › 726 › matinfo.html
Matlab code for finding size of matrices
Here is how to get information about the dimensions of a vector or matrix. In the following A is an m x n matrix, x is a 1 x n row vector and y is a m x 1 column vector. size(A,1) % Number of rows of A = m size(A,2) % Number of columns of A = n size(A) % m n where A is m x n lenght(A) % max(m,n) length(x) % m length(y) % n
🌐
MathWorks
mathworks.com › matlabcentral › answers › 639120-counting-the-size-of-x-indices-of-a-matrix
Counting the Size of X Indices of a Matrix - MATLAB Answers - MATLAB Central
November 6, 2020 - So for example, if I have matrix A which is 4x5x2 (for whatever reason), I want a series of operations that will output: Dimension A = 4, Dimension B = 5, Dimension C = 2". I have been trying to do this with "F = Size(A)", except I have to input "[a, b, c] = Size(A)" to get the output I want. And I can't find a way to input a matrix (so that it identifies the size of each dimension) without already saying how many dimensions there are.
🌐
Matlab Tutorial
matlab11.wordpress.com › lectures › basic-functions
Basic Functions | Matlab Tutorial - WordPress.com
October 9, 2011 - Note that dimension 1 is the number of rows while dimension 2 is the number of columns, so if x is T by K, z=size(x,1) returns T while z=size(x,2) returns K . alternatively in a better way ... This command find the size of the maximum dimension of x. YOU WILL NOT get the dimension (BE CAREFUL!). ... Note: If x is T by K , T > K , z = T . If K > T , z = K. To compute the sum matrix, use the command sum(x). ... dim as always the same meaning as above and in the all MATLAB enviroment.
🌐
MathWorks
mathworks.com › instrument control toolbox › driver-based instrument communication › generic instrument drivers
size - Size of instrument object array - MATLAB
[m,n] = size(obj) returns the number of rows and columns in separate output variables. [m1,m2,m3,...,mn] = size(obj) returns the length of the first n dimensions of obj.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1971659-how-to-expand-dimension-of-a-2d-array
How to expand dimension of a 2D array - MATLAB Answers - MATLAB Central
May 23, 2023 - I have a 2D array M with dimension 60, 60. However, I want to expand the array dimension to 60, 60, 1. I tried by reshape which simply doesn't work for dimension 1 at the end/3rd. M=reshape(M,[...