- Main Page
- Tutorials
- Functions
- Release Notes
- Examples

Indexing

There are several ways of referencing values. ArrayFire uses parenthesis for subscripted referencing instead of the traditional square bracket notation. Indexing is zero-based, i.e. the first element is at index zero (`A(0)`). Indexing can be done with mixtures of:

- integer scalars
- seq() representing a linear sequence
- end representing the last element of a dimension
- span representing the entire dimension
- row(i) or col(i) specifying a single row/column
- rows(first,last) or cols(first,last) specifying a span of rows or columns

See Indexing for the full listing.

array A = array(seq(1,9), 3, 3);
af_print(A);
af_print(A(0)); // first element
af_print(A(0,1)); // first row, second column
af_print(A(end)); // last element
af_print(A(-1)); // also last element
af_print(A(end-1)); // second-to-last element
af_print(A(1,span)); // second row
af_print(A.row(end)); // last row
af_print(A.cols(1,end)); // all but first column
float b_host[] = {0,1,2,3,4,5,6,7,8,9};
array b(10, 1, b_host);
af_print(b(seq(3)));
af_print(b(seq(1,7)));
af_print(b(seq(1,7,2)));
af_print(b(seq(0,end,2)));

You can set values in an array:

array A = constant(0, 3, 3);
af_print(A);
// setting entries to a constant
A(span) = 4; // fill entire array
af_print(A);
A.row(0) = -1; // first row
af_print(A);
A(seq(3)) = 3.1415; // first three elements
af_print(A);
// copy in another matrix
array B = constant(1, 4, 4, s32);
B.row(0) = randu(1, 4, f32); // set a row to random values (also upcast)

Use one array to reference into another.

float h_inds[] = {0, 4, 2, 1}; // zero-based indexing
array inds(1, 4, h_inds);
af_print(inds);
array B = randu(1, 4);
af_print(B);
array c = B(inds); // get
af_print(c);
B(inds) = -1; // set to scalar
B(inds) = constant(0, 4); // zero indices
af_print(B);