- 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.3 Comma Separated Lists

Comma separated lists are the basic argument type to all Octave functions. In the example

max (a, b)

a, b is a comma separated list. Comma separated lists can appear on both the right and left hand side of an equation. For example

[i, j] = ceil (find (x, [], "last"));

where i, j is equally a comma separated list. Comma separated lists cannot be directly manipulated by the user. However, both structures and cell arrays can be converted into comma separated lists, which makes them useful to keep the input arguments and return values of functions organized. Another example of where a comma separated list can be used is in the creation of a new array. If all the accessed elements of a cell array are scalars or column vectors, they can be concatenated into a new column vector containing the elements, by surrounding the list with [ and ] as in the following example

a = {1, [2, 3], 4};
b = [a{:}]
     => b =
         1   2   3   4

It is also possible to pass the accessed elements directly to a function. The list of elements from the cell array will be passed as an argument list to a given function as if it is called with the elements as arguments. The two calls to printf in the following example are identical but the latter is simpler and handles more situations

c = {"GNU", "Octave", "is", "Free", "Software"};
printf ("%s ", c{1}, c{2}, c{3}, c{4}, c{5});
     -| GNU Octave is Free Software 
printf ("%s ", c{:});
     -| GNU Octave is Free Software 

Just like it is possible to create a numerical array from selected elements of a cell array, it is possible to create a new cell array containing the selected elements. By surrounding the list with ‘{’ and ‘}’ a new cell array will be created, as the following example illustrates

a = {1, rand(2, 2), "three"};
b = { a{ [1, 3] } }
     => b =
         {
           [1,1] =  1
           [1,2] = three
         }

This syntax is however a bit cumbersome, and since this is a common operation, it is possible to achieve the same using the ‘(’ and ‘)’ operators for indexing. When a cell array is indexed using the ‘(’ and ‘)’ operators a new cell array containing the selected elements is returned. Using this syntax, the previous example can be simplified into the following

a = {1, rand(2, 2), "three"};
b = a( [1, 3] )
     => b =
         {
           [1,1] =  1
           [1,2] = three
         }

A comma separated list can equally appear on the left-hand side of an assignment. An example is

in {1} = ceil (rand (10, 1));
in {2} = [];
in {3} = "last";
in {4} = "first";
out = cell (4, 1);
[out{1:2}] = find (in{1 : 3});
[out{3:4}] = find (in{[1, 2, 4]});

Structure arrays can equally be used to create comma separated lists. This is done by addressing one of the fields of a structure array. For example

x = ceil (randn (10, 1)); 
in = struct ("call1", {x, Inf, "last"}, 
             "call2", {x, Inf, "first"});
out = struct ("call1", cell (2, 1), "call2", cell (2, 1));
[out.call1] = find (in.call1);
[out.call2] = find (in.call2);
ISBN 095461206XGNU Octave Manual Version 3See the print edition