Next: Indexing Cell Arrays, Previous: Basic Usage of Cell Arrays, Up: Cell Arrays [Contents][Index]
The introductory example (see Basic Usage of Cell Arrays) showed how to create a cell array containing currently available variables. In many situations, however, it is useful to create a cell array and then fill it with data.
The cell
function returns a cell array of a given size, containing
empty matrices. This function is similar to the zeros
function for creating new numerical arrays. The following example creates
a 2-by-2 cell array containing empty matrices
c = cell (2,2) ⇒ c = { [1,1] = [](0x0) [2,1] = [](0x0) [1,2] = [](0x0) [2,2] = [](0x0) }
Just like numerical arrays, cell arrays can be multi-dimensional. The
cell
function accepts any number of positive integers to describe
the size of the returned cell array. It is also possible to set the size
of the cell array through a vector of positive integers. In the
following example two cell arrays of equal size are created, and the size
of the first one is displayed
c1 = cell (3, 4, 5); c2 = cell ( [3, 4, 5] ); size (c1) ⇒ ans = 3 4 5
As can be seen, the size function also works for cell arrays. As do other functions describing the size of an object, such as length, numel, rows, and columns.
Create a new cell array object.
If invoked with a single scalar integer argument, return a square NxN cell array. If invoked with two or more scalar integer arguments, or a vector of integer values, return an array with the given dimensions.
See also: cellstr, mat2cell, num2cell, struct2cell.
As an alternative to creating empty cell arrays, and then filling them, it
is possible to convert numerical arrays into cell arrays using the
num2cell
, mat2cell
and cellslices
functions.
Convert the numeric matrix A to a cell array.
When no dim is specified, each element of A becomes a 1x1 element in the output C.
If dim is defined then individual elements of C contain all of the elements from A along the specified dimension. dim may also be a vector of dimensions with the same rule applied.
For example:
x = [1,2;3,4] ⇒ 1 2 3 4 ## each element of A becomes a 1x1 element of C num2cell (x) ⇒ { [1,1] = 1 [2,1] = 3 [1,2] = 2 [2,2] = 4 } ## all rows (dim 1) of A appear in each element of C num2cell (x, 1) ⇒ { [1,1] = 1 3 [1,2] = 2 4 } ## all columns (dim 2) of A appear in each element of C num2cell (x, 2) ⇒ { [1,1] = 1 2 [2,1] = 3 4 } ## all rows and cols appear in each element of C ## (hence, only 1 output) num2cell (x, [1, 2]) ⇒ { [1,1] = 1 2 3 4 }
See also: mat2cell.
Convert the matrix A to a cell array.
Each dimension argument (dim1, dim2, etc.) is a vector of
integers which specifies how to divide that dimension’s elements amongst the
new elements in the output C. The number of elements in the i-th
dimension is size (A, i)
. Because all elements in A
must be partitioned, there is a requirement that sum (di) == size
(A, i)
. The size of the output cell C is numel (dim1) x
numel (dim2) x … x numel (dimn).
Given a single dimensional argument, rowdim, the output is divided into rows as specified. All other dimensions are not divided and thus all columns (dim 2), pages (dim 3), etc. appear in each output element.
Examples
x = reshape (1:12, [3, 4])' ⇒ 1 2 3 4 5 6 7 8 9 10 11 12
## The 4 rows (dim1) are divided in to two cell elements ## with 2 rows each. ## The 3 cols (dim2) are divided in to three cell elements ## with 1 col each. mat2cell (x, [2,2], [1,1,1]) ⇒ { [1,1] = 1 4 [2,1] = 7 10 [1,2] = 2 5 [2,2] = 8 11 [1,3] = 3 6 [2,3] = 9 12 }
## The 4 rows (dim1) are divided in to two cell elements ## with a 3/1 split. ## All columns appear in each output element. mat2cell (x, [3,1]) ⇒ { [1,1] = 1 2 3 4 5 6 7 8 9 [2,1] = 10 11 12 }
Given an array x, this function produces a cell array of slices from the array determined by the index vectors lb, ub, for lower and upper bounds, respectively.
In other words, it is equivalent to the following code:
n = length (lb); sl = cell (1, n); for i = 1:length (lb) sl{i} = x(:,…,lb(i):ub(i),…,:); endfor
The position of the index is determined by dim. If not specified, slicing is done along the first non-singleton dimension.
See also: cell2mat, cellindexmat, cellfun.
Next: Indexing Cell Arrays, Previous: Basic Usage of Cell Arrays, Up: Cell Arrays [Contents][Index]