 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:

See Assignment & Indexing operation on arrays for the full listing.

array A = array(seq(1,9), 3, 3);
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);
// setting entries to a constant
A(span) = 4; // fill entire array
A.row(0) = -1; // first row
A(seq(3)) = 3.1415; // first three elements
// 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);
array c = B(inds); // get
B(inds) = -1; // set to scalar
B(inds) = constant(0, 4); // zero indices