Getting Started |
Cell Arrays
Cell arrays in MATLAB are multidimensional arrays whose elements are copies of other arrays. A cell array of empty matrices can be created with the cell
function. But, more often, cell arrays are created by enclosing a miscellaneous collection of things in curly braces, {}
. The curly braces are also used with subscripts to access the contents of various cells. For example,
produces a 1-by-3 cell array. The three cells contain the magic square, the row vector of column sums, and the product of all its elements. When C
is displayed, you see
This is because the first two cells are too large to print in this limited space, but the third cell contains only a single number, 16!, so there is room to print it.
Here are two important points to remember. First, to retrieve the contents of one of the cells, use subscripts in curly braces. For example, C{1}
retrieves the magic square and C{3}
is 16. Second, cell arrays contain copies of other arrays, not pointers to those arrays. If you subsequently change A
, nothing happens to C
.
You can use three-dimensional arrays to store a sequence of matrices of the same size. Cell arrays can be used to store a sequence of matrices of different sizes. For example,
produces a sequence of magic squares of different order.
M = [ 1] [ 2x2 double] [ 3x3 double] [ 4x4 double] [ 5x5 double] [ 6x6 double] [ 7x7 double] [ 8x8 double]
You can retrieve the 4-by-4 magic square matrix with
Other Data Structures | Characters and Text |
© 1994-2005 The MathWorks, Inc.