array shape
stride array
location of the first indexed value based on the stride array
specifies whether an array is row-major (C-style) or column-major (Fortran-style)
linear index
specifies how to handle a linear index which exceeds array dimensions
subscripts
var shape = [ 3, 3, 3 ];
var strides = [ 9, 6, 1 ];
var offset = 0;
var order = 'row-major';
var s = ind2sub( shape, strides, offset, order, 17, 'throw' );
// returns [ 1, 2, 2 ]
Converts a linear index to an array of subscripts.
Notes
The function accepts the following "modes":
throw
: throws an error when a linear index exceeds array dimensions.wrap
: wrap around a linear index exceeding array dimensions using modulo arithmetic.clamp
: set a linear index exceeding array dimensions to either0
(minimum linear index) or the maximum linear index.When provided a stride array containing negative strides, if an
offset
is greater than0
, the function interprets the linear index as an index into the underlying data buffer for the array, thus returning subscripts from the perspective of that buffer. If anoffset
is equal to0
, the function treats the linear index as an index into an array view, thus returning subscripts from the perspective of that view.var shape = [ 2, 2 ]; var order = 'row-major'; var strides = [ -2, 1 ]; var offset = 2; var mode = 'throw'; // From the perspective of a view... var s = ind2sub( shape, strides, 0, order, 0, mode ); // returns [ 0, 0 ] s = ind2sub( shape, strides, 0, order, 1, mode ); // returns [ 0, 1 ] s = ind2sub( shape, strides, 0, order, 2, mode ); // returns [ 1, 0 ] s = ind2sub( shape, strides, 0, order, 3, mode ); // returns [ 1, 1 ] // From the perspective of an underlying buffer... s = ind2sub( shape, strides, offset, order, 0, mode ); // returns [ 1, 0 ] s = ind2sub( shape, strides, offset, order, 1, mode ); // returns [ 1, 1 ] s = ind2sub( shape, strides, offset, order, 2, mode ); // returns [ 0, 0 ] s = ind2sub( shape, strides, offset, order, 3, mode ); // returns [ 0, 1 ]
In short, from the perspective of a view, view data is always ordered.
linear index must not exceed array dimensions