Subscripted array indexing

Get or set part of an array. More...

Data Structures

class  seq
 sequential index generator More...
 

Modules

 row/rows
 Access one or more rows of an array.
 
 col/cols
 Access one or more columns of an array.
 
 slice/slices
 Access one or more slices of an array.
 
 operator()
 Indexing using the () operator.
 
 operator=
 Assignment operator.
 
 end
 Reference last element in dimension.
 
 span
 Reference the entire dimension.
 

Detailed Description

Get or set part of an array.

Each array has a dim4 that specifies the size of the array along each of the 4 dimensions. The size of each dimension is alteast 1.

The most common ways to index are using a specific index locations, seq or using an array of indices.

Other variables that are commonly used are span, and end. Span is used to reference the entire dimension. End is used to specify the last index in the dimension.

Use of negative indices or indices greater than the size of the dimensions for subscripting results in undefined behavior.

For more examples, see Indexing .

array a = randu(3,4);
print(a);
print(a.row(1)); // second row
print(a.rows(0,1)); // first and second rows
print(a.col(0)); // first column (just move pointer, i.e. no-op)
print(a.cols(1,3)); // second, third, fourth columns (just move pointer, i.e. no-op)
print(a(4));
print(a(seq(3)));
a.col(end) = -1; // set last column
print(a);
print(a(a > .5));

produces:

a =
        0.7402      0.9690      0.6673      0.5132
        0.9210      0.9251      0.1099      0.7762
        0.0390      0.4464      0.4702      0.2948

a.row(1) =
        0.9210      0.9251      0.1099      0.7762

a.rows(0,1) =
        0.7402      0.9690      0.6673      0.5132
        0.9210      0.9251      0.1099      0.7762

a.col(0) =
        0.7402
        0.9210
        0.0390

a.cols(1,3) =
        0.9690      0.6673      0.5132
        0.9251      0.1099      0.7762
        0.4464      0.4702      0.2948

a(4) =
        0.9251

a(seq(3)) =
        0.7402
        0.9210
        0.0390

a.col(end) = -1
a =
        0.7402      0.9690      0.6673     -1.0000
        0.9210      0.9251      0.1099     -1.0000
        0.0390      0.4464      0.4702     -1.0000

a(a > .5) =
        0.7402
        0.9210
        0.9690
        0.9251
        0.6673

Use seq objects to create numeric sequences for subscripting or arithmetic. See also Indexing .

array A = randu(5,1);
array B = A(seq(0,3)); // extract elements {0,1,2,3}

Sequences can also be combined with arithmetic after casting:

randu(1,5) + array(seq(3, 3, 16));
See Also
Indexing

For examples, see Indexing .