size — Return array dimension sizes in vector or multiple-output forms.
size reads outer array shape metadata, supports scalar, vector, and separate dimension queries, and implements requested-output collapse rules.
Syntax
sz = size(A)
sz = size(A, dim)
sz = size(A, dim1, dim2, ...)
[sz1, ..., szN] = size(A)
[sz1, ..., szN] = size(A, dim1, ..., dimN)Inputs
A- Type
Any- Required
- Yes
- Description
- Value whose MATLAB-visible dimensions are inspected.
dim- Type
SizeArg- Required
- Variadic
- Description
- Positive integer scalar selectors, or one vector or empty selector.
Returns
sz- Type
NumericArray- Description
- Host double row vector containing the queried dimension extents.
sz- Type
NumericScalar- Description
- Host double scalar for each requested output.
Returned values from size depend on how many outputs the caller requests.
Errors
RunMat:size:InvalidDimensionType- When
- A selector is not a numeric scalar or vector.
- Message
size: dimension selectors must be numeric scalars or one numeric vector
RunMat:size:InvalidDimensionVector- When
- The single vector selector is not vector-shaped.
- Message
size: dimension selector must be a vector
RunMat:size:InvalidDimensionList- When
- A vector occurs within a variadic scalar selector list.
- Message
size: separate dimension arguments must be scalars
Dimension queries
size(A) returns the complete MATLAB-visible shape as a 1-by-N host double row vector. Scalars and string scalars have shape 1-by-1; character vectors retain their character-array shape; cells, structs, object arrays, tables, and timetables report their outer array dimensions.
size(A, dim) returns one extent. A vector selects several dimensions, an empty vector returns a 1-by-0 row, and separate scalar arguments select several dimensions. A selector beyond the visible rank contributes 1. The vector and separate-argument forms are available in the compatible language surface introduced in R2019b.
Multiple outputs
With no explicit dimensions, multiple outputs receive leading dimensions separately. If fewer outputs than visible dimensions are requested, the final output is the checked product of every remaining dimension. Extra outputs are padded with 1.
With explicit queried dimensions, the number of outputs must equal the number of queried dimensions. Every output is a host double scalar.
Exact structural values
All fixed-width integer selector classes are decoded from native storage without conversion through double. Extents, selected dimensions, and collapsed products use checked structural arithmetic. RunMat reports an error instead of rounding a structural result that cannot be represented exactly by the documented double output.
gpuArray and distributed inputs are inspected through validated handle metadata. size launches no kernel, calls no acceleration provider, transfers no payload, and returns host values at a fusion boundary.
Examples
Read a matrix shape
A = [1 2 3; 4 5 6];
sz = size(A)Expected output:
sz = [2 3]Read one dimension
A = randn(8, 4);
rows = size(A, 1)Expected output:
rows = 8Read selected dimensions
A = zeros(5, 4, 3);
selected = size(A, [1 3])Expected output:
selected = [5 3]Use separate dimension selectors
A = zeros(5, 4, 3);
selected = size(A, 2, 3)Expected output:
selected = [4 3]Collapse trailing dimensions into the final output
A = zeros(3, 4, 5);
[first, remainder] = size(A)Expected output:
first = 3; remainder = 20Return an empty dimension query
A = ones(2, 3);
sz = size(A, [])Expected output:
sz is a 1-by-0 double arrayInspect a cell array
C = {1, 2, 3; 4, 5, 6};
sz = size(C)Expected output:
sz = [2 3]Inspect a resident array without gathering
G = gpuArray(ones(256, 512));
sz = size(G)Expected output:
sz = [256 512]Using size with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how size changes the result.
Run a small size example, explain the result, then change one input and compare the output.
FAQ
How is size different from length and numel?⌄
size exposes individual dimensions, length returns the largest dimension, and numel returns the product of all dimensions.
What happens when a selector exceeds the array rank?⌄
The corresponding result is 1.
How are row and column vectors reported?⌄
A row vector is 1-by-N and a column vector is N-by-1.
What happens for empty arrays?⌄
Zero extents remain present in the returned shape and participate in collapsed products.
Can dimensions be queried as a vector or list?⌄
Yes. A vector or separate positive integer scalar selectors return the corresponding extents in order.
Does size gather gpuArray or distributed data?⌄
No. Complete shape metadata is part of each validated handle.
Where does the result live?⌄
All size outputs are host double values because they describe metadata, not array payload.
Related Array functions
Introspection
height · iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · width
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how size is executed, line by line, in Rust.
- View the source for size in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.