Main Content

Manipulate Multidimensional Numerical Arrays in C MEX Files

You can manipulate multidimensional numerical arrays by using typed data access functions like mxGetDoubles and mxGetComplexDoubles. The example findnz.c takes an N-dimensional array of doubles and returns the indices for the nonzero elements in the array.

Build the example.

mex -R2018a findnz.c

Create a sample matrix.

matrix = [ 3 0 9 0; 0 8 2 4; 0 9 2 4; 3 0 9 3; 9 9 2 0]
matrix =
     3     0     9     0
     0     8     2     4
     0     9     2     4
     3     0     9     3
     9     9     2     0

findnz determines the position of all nonzero elements in the matrix.

nz = findnz(matrix)
nz =
     1     1
     4     1
     5     1
     2     2
     3     2
     5     2
     1     3
     2     3
     3     3
     4     3
     5     3
     2     4
     3     4
     4     4

See Also

|

Related Topics