What does the size function do in MATLAB / RunMat?
size(A) reports the length of every dimension of A. The return value is always
a row vector describing the current shape, with scalars reported as 1×1. This matches
MathWorks MATLAB semantics for scalars, vectors, matrices, N-D arrays, strings, chars,
tables, and cells.
How does the size function behave in MATLAB / RunMat?
size(A)returns a1×Nrow vector listing the extents of each dimension.size(A, dim)returns the extent of the specified dimension as a scalar. Dimensions outside the current rank return1.size(A, dims)accepts a vector of dimensions and returns the corresponding extents.- Column and row vectors are distinguished by the tensor metadata:
size(A)yields[m n]form×nmatrices,[1 n]for row vectors, and[m 1]for column vectors. - Character arrays return
[rows cols]; string scalars return[1 1]. - Cell arrays and struct scalars follow MATLAB's array semantics.
- Empty dimensions (zeros) are preserved in the output vector.
- Multiple output variables follow MATLAB rules:
[m, n, p] = size(A)peels off leading dimensions, padding with1s when the array has fewer than the requested outputs.
size Function GPU Execution Behavior
When a value already resides on the GPU, size reads the shape metadata carried
by the GPU tensor handle. No kernels are launched and no device data is gathered.
If a provider does not populate the shape field on its handles, RunMat falls back
to materialising the tensor on the host so the answer stays correct. The builtin
never allocates device memory and always returns a host double array, making it safe
to call inside fused GPU expressions without breaking residency.
Examples of using the size function in MATLAB / RunMat
Find the size of a matrix
A = [1 2 3; 4 5 6];
shape = size(A);
Expected output:
shape = [2 3];
Determine the number of rows in a matrix
A = randn(8, 4);
m = size(A, 1);
Expected output:
m = 8;
Query multiple dimensions at once
A = rand(5, 4, 3);
selected = size(A, [1 3]);
Expected output:
selected = [5 3];
Inspect a gpuArray without gathering
G = gpuArray(ones(256, 512));
shape = size(G);
Expected output:
shape = [256 512];
Check the size of a cell array
C = {1, 2, 3; 4, 5, 6};
grid = size(C);
Expected output:
grid = [2 3];
FAQ
How is size different from length?
length(A) returns the length of the largest dimension, whereas size(A) returns every dimension.
What happens when I request a dimension larger than the array rank?
MATLAB semantics apply: size(A, dim) returns 1 when dim exceeds the number of stored dimensions.
Can I pass a vector of dimensions?
Yes. size(A, [1 3]) returns a row vector with the size of the first and third dimensions.
Does size gather GPU data?
No. The runtime reads shape metadata stored alongside the GPU buffer. Gathering only happens if a provider fails to report the shape, in which case RunMat downloads the tensor before continuing.
How are row and column vectors reported?
Row vectors are [1 n], column vectors are [n 1], matching MATLAB.
What about empty arrays?
Empty dimensions (0-length) are preserved exactly in the returned vector.
Can I pass non-integer dimensions?
No. The dimension argument must be positive integers; fractions or negatives raise an error.
How can I check multiple dimensions and keep the result on the GPU?
size always returns a host double array because it is metadata. Use the values to plan GPU computations, but no GPU array is produced.
See Also
length (MathWorks), ndims (MathWorks), numel (MathWorks), MathWorks size reference