size — Get the dimensions of scalars, vectors, matrices, and N-D arrays.
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 size works in 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.
How size runs on the GPU
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
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 = 8Query 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.
Related functions to explore
These functions work well alongside size. Each page has runnable examples you can try in the browser.
length, ndims, numel, size, isempty, ismatrix, isscalar, isvector, length, ndims, numel
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how size works, line by line, in Rust.
- View size.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.