View all functions

CategoryCells: Core

What does the cell function do in MATLAB / RunMat?

cell constructs cell arrays—containers that can hold heterogeneous values in each element. Every cell starts out containing an empty value: by default the empty double array [], or an empty value that matches a 'like' prototype when the prototype lives on the host. GPU-backed prototypes currently fall back to empty double arrays on the host. RunMat mirrors MATLAB's behaviour for scalar and arbitrary size forms (cell, cell(n), cell(m, n, p, ...), cell([m n p ...]), cell(size(A)), and cell(___, 'like', prototype)).

How does the cell function behave in MATLAB / RunMat?

  • cell (with no arguments) returns a 0×0 empty cell array.
  • cell(n) returns an n×n cell array whose elements default to the empty value.
  • cell(m, n, p, ...) accepts any number of size scalars and returns an N-D cell array whose dimensions match the arguments.
  • cell(sz) or cell(size(A)) accepts a numeric size vector of any length. A single-element vector produces an m×1 cell array; longer vectors create higher-dimensional cell arrays matching the provided extents.
  • cell(___, 'like', prototype) copies the prototype's size when no explicit dimensions are supplied and fills each element with the empty value that matches the prototype's class (for example logical empty arrays, complex empties, or empty strings). When the prototype is a gpuArray, RunMat keeps the host-empty double fallback until GPU-resident cell storage ships.
  • Size arguments must be finite, non-negative integers that are representable in double precision.

cell Function GPU Execution Behaviour

Cell arrays live on the CPU heap because each element can contain arbitrarily typed values that may require garbage-collected storage. When cell is invoked inside a fused GPU expression RunMat terminates the fusion group, gathers any GPU-resident size inputs, and creates the cell array on the host. 'Like' prototypes that refer to gpuArray inputs are honoured for shape inference. Each cell currently receives an empty double array even if the prototype itself is a gpuArray; this fallback will disappear once device-side cell residency lands. Once the acceleration layer adds true GPU cells, providers can register hooks without changing user code.

Examples of using the cell function in MATLAB / RunMat

Creating a square cell array of empty placeholders

C = cell(3);
size(C)

Expected output:

ans =
     3     3

Creating a rectangular 2-by-4 cell array

C = cell(2, 4);
class(C)

Expected output:

ans =
    'cell'

Creating a 3-D cell array

C = cell(2, 3, 4);
size(C)

Expected output:

ans =
     2     3     4

Matching the size of an existing matrix

A = ones(5, 2);
C = cell(size(A));
size(C)

Expected output:

ans =
     5     2

Creating a cell array using a 'like' prototype

proto = logical.empty(0, 0);
C = cell(2, 3, 'like', proto);
cellfun(@isempty, C)

Expected output:

ans =
     1     1     1
     1     1     1

Using a column size vector

sz = [4; 1];
C = cell(sz);
size(C)

Expected output:

ans =
     4     1

Verifying that every cell starts with []

C = cell(2, 2);
isequal(C{1,1}, [], C{2,2}, [])

Expected output:

ans = logical
     1

GPU residency in RunMat (Do I need gpuArray?)

No. Cell arrays are created on the host regardless of the residency of the size arguments. If the inputs happen to live on the GPU (for example, a gpuArray size vector), RunMat gathers those values before allocating the cell array. The result is always a host cell array; downstream code can still place tensors or gpuArrays inside individual cells as needed.

FAQ

What values do new cells contain?

Each cell starts with an empty value. By default that is the empty double array []. When you supply a 'like' prototype, RunMat chooses the matching empty logical, complex, string, character, or cell value instead. GPU-backed prototypes currently fall back to empty double arrays on the host until device-resident cell elements are supported.

Are negative or fractional sizes allowed?

No. Every size argument must be a finite, non-negative integer that can be represented exactly in double precision.

Can I create zero-sized cell arrays?

Yes. You can use cell(0), cell(0, n), or supply a size vector containing zeros. RunMat returns an empty cell array with the requested shape.

Do GPU prototypes change how cell behaves?

A 'like' prototype that resides on the GPU is gathered automatically. Shape inference follows the prototype, and each cell receives an empty double array on the host while GPU cell storage is still under development.

What about N-dimensional cell arrays?

RunMat supports N-dimensional cell arrays. Supply as many scalar dimensions or size-vector entries as you need and the builtin constructs a cell array with that exact shape. Trailing singleton dimensions are preserved so downstream code can introspect the full size vector.

Does cell copy data from existing containers?

No. The builtin only allocates empty cells. Populate the elements afterwards with assignments such as C{1,2} = magic(3);.

See Also

num2cell, cellfun, struct, gpuArray, gather

Source & Feedback