- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

6.2.2 Indexing Cell Arrays

As shown in the introductory example elements can be inserted from cell arrays using the ‘{’ and ‘}’ operators. Besides the change of operators, indexing works for cell arrays like for multidimensional arrays. As an example, all the rows of the first and third column of a cell array can be set to 0 with the following code

c{:, [1, 3]} = 0;

Accessing values in a cell array is, however, different from the same operation for numerical arrays. Accessing a single element of a cell array is very similar to numerical arrays, for example

element = c{1, 2};

This will, however, not work when accessing multiple elements of a cell array, because it might not be possible to represent all elements with a single variable as is the case with numerical arrays.

Accessing multiple elements of a cell array with the ‘{’ and ‘}’ operators will result in a comma-separated list (see section 6.3 Comma Separated Lists) of all the requested elements as discussed later.

One distinction between ‘{’ and ‘(’ to index cell arrays is in the deletion of elements from the cell array. In a similar manner to a numerical array the ‘()’ operator can be used to delete elements from the cell array. The ‘{}’ operator however will remove the elements of the cell array, but not delete the space for them. For example

x = {"1", "2"; "3", "4"};
x{1, :} = []
=> x =
        [1,1] = [](0x0)
        [2,1] = 3
        [1,2] = [](0x0)
        [2,2] = 4

x(1, :) = []
=> x =
        [1,1] = 3
        [1,2] = 4
ISBN 095461206XGNU Octave Manual Version 3See the print edition