Main Content

mxCalcSingleSubscript (C)

Offset from first element to desired element

C Syntax

#include "matrix.h"
mwIndex mxCalcSingleSubscript(const mxArray *pm, mwSize nsubs, mwIndex *subs);

Description

Call mxCalcSingleSubscript to determine how many elements there are between the beginning of the mxArray and a given element of that mxArray. The function converts subscripts to linear indices.

For example, given a subscript like (5,7), mxCalcSingleSubscript returns the distance from the first element of the array to the (5,7) element. Remember that the mxArray data type internally represents all data elements in a one-dimensional array no matter how many dimensions the MATLAB® mxArray appears to have. For examples showing the internal representation, see Data Storage.

Avoid using mxCalcSingleSubscript to traverse the elements of an array. In C, it is more efficient to find the starting address of the array and then use pointer autoincrementing to access successive elements. For example, to find the starting address of a numerical array, call one of the typed data access functions, for example, mxGetDoubles or mxGetComplexDoubles.

Input Arguments

expand all

Pointer to an mxArray array, specified as const mxArray*.

Number of elements in the subs array, specified as mwSize. Typically, you set nsubs equal to the number of dimensions in the mxArray that pm points to.

Array of subscripts, specified as mwIndex. Each value in the array specifies that dimension's subscript. The value in subs(1) specifies the row subscript, and the value in subs(2) specifies the column subscript. Use 1-based indexing for subscripts. For example, to express the starting element of a two-dimensional mxArray in subs, set subs(1) to 1 and subs(2) to 1.

Examples

See these examples in matlabroot/extern/examples/mx:

Introduced before R2006a