cell2mat — Concatenate compatible cell-array blocks into one dense array.
cell2mat assembles array blocks using the cell grid as a block layout.
Syntax
A = cell2mat(C)Inputs
C- Type
Any- Required
- Yes
- Description
- Cell array containing compatible array blocks.
Returns
A- Type
Any- Description
- Dense array assembled from the cell contents.
Errors
RunMat:cell2mat:TooManyOutputs- When
- More than one output is requested.
- Message
cell2mat: too many output arguments
RunMat:cell2mat:InvalidInput- When
- The input is not a cell array.
- Message
cell2mat: expected a cell array input
RunMat:cell2mat:InvalidContents- When
- The cell contents cannot form one dense array.
- Message
cell2mat: cell contents are not compatible for concatenation
Block concatenation
cell2mat(C) concatenates compatible numeric, logical, complex, or character blocks from a cell array. The cell grid selects each block's position in the result.
Along every dimension of the cell grid, blocks sharing the same grid coordinate must agree on their extent in that dimension. Extents may differ between coordinates, including page coordinates of an N-D cell grid, and those extents add to form the output shape. Only block dimensions beyond the cell-grid rank must agree throughout the grid. Empty blocks contribute zero extent.
Classes and storage
The result keeps the content class. Fixed-width integer blocks retain exact storage; compatible mixed integer blocks use the leftmost nonempty integer class and saturating assignment conversion. Scalar doubles cannot be combined with int64 or uint64 blocks.
Strings, nested cells, structs, and objects are not valid contents. Character blocks produce a character array and must remain two-dimensional.
Execution
Cell arrays are host containers. Resident numeric elements are gathered before concatenation, and the result is host-resident. cell2mat ends an active fusion group.
An empty cell array returns a 0-by-0 double array.
Examples
Tile an N-D cell grid with unequal row and page extents
C = cell(2,1,2);
C{1,1,1} = reshape(1:4,[1 2 2]);
C{2,1,1} = reshape(5:12,[2 2 2]);
C{1,1,2} = reshape(13:18,[1 2 3]);
C{2,1,2} = reshape(19:30,[2 2 3]);
A = cell2mat(C);Expected output:
A has shape 3-by-2-by-5Convert scalar cells
C = {1, 2; 3, 4};
A = cell2mat(C);Expected output:
A is [1 2; 3 4]Concatenate rectangular blocks
C = {[1 2], [3 4 5]; [6 7], [8 9 10]};
A = cell2mat(C);Expected output:
A is a 2-by-5 matrixPreserve logical storage
C = {true(2,1), false(2,1)};
A = cell2mat(C);Expected output:
A is a 2-by-2 logical arrayConcatenate complex blocks
C = {1+2i, [3+4i 5+6i]};
A = cell2mat(C);Expected output:
A contains three complex valuesJoin character rows
C = {'foo', 'bar'; 'baz', 'qux'};
A = cell2mat(C);Expected output:
A has rows 'foobar' and 'bazqux'Tile N-dimensional blocks
C = {ones(2,2,3), 2*ones(2,1,3)};
A = cell2mat(C);Expected output:
A has shape 2-by-3-by-3Convert an empty cell grid
C = {[], []; [], []};
A = cell2mat(C);Expected output:
A is a 0-by-0 double arrayPreserve wide integer values
C = {0x0020000000000001u64, 0xFFFFFFFFFFFFFFFFu64};
A = cell2mat(C);Expected output:
A is an exact 1-by-2 uint64 arrayUsing cell2mat with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cell2mat changes the result.
Run a small cell2mat example, explain the result, then change one input and compare the output.
FAQ
Which cell contents can cell2mat concatenate?⌄
Numeric, logical, complex, and character arrays are supported. Every nonempty block must belong to one compatible fundamental kind; nested cells, strings, structs, and objects are rejected.
Must every block have the same shape?⌄
No. Widths may differ between cell-grid columns and heights may differ between rows. Extents must agree wherever blocks share the corresponding grid coordinate.
What happens when cells contain empty arrays?⌄
Empty blocks contribute zero extent along their tiling dimension while retaining the selected content class. A completely empty cell array returns a 0-by-0 double array.
Does cell2mat preserve GPU residency?⌄
Not currently. Resident elements gather and the concatenated result is allocated on the host.
Related Cells functions
Source you can read
Every RunMat function's source is public. Read exactly how cell2mat is executed, line by line, in Rust.
- View the source for cell2mat 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.