MATLAB: Mex Code for Array Slicing

array slicingMATLAB Compilermex compiler

Hello,
Thank you for your help.
I have a Matlab 3D array (buildSpaceGrid) 300 * 300 * 600. During my algorithm, i need to access subarrays of it several times. It is very inefficient to get such arrays with simple array slicing.
For example, i tried this, where build Space Grid is my 3D array, ixx,iyy, and izz are the starting x,y and z coordinates in the 3 dimensions.
buildSpaceGrid( ixx:ixx+partSize(1)-1,...
iyy:iyy+partSize(2)-1,...
izz:izz+partSize(3)-1)
It seems doing this type of array slicing is like death sentence in Matlab in terms of performance. I have to do this several times during my algorithm for different values of ixx, iyy and izz while my 3D array also updates as the algorithm progresses.
I decided to write a mex routine in c++ to make this more efficient. The routine takes the 3D array, buildSpaceGrid, ixx,iyy,izz and partsize and outputs the subarray.
#include "mex.h"
#include "matrix.h"
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mwSize nDimNum;
const mwSize *pSize;
bool *pD;
mxArray *Data;
bool *buildSpaceGrid;
int *ixx;
int *iyy;
int *izz;
mwSize i, j, k;
buildSpaceGrid = mxGetLogicals(prhs[0]);
ixx = (int *)mxGetPr(prhs[1]);
iyy = (int *)mxGetPr(prhs[1]);
izz = (int *)mxGetPr(prhs[3]);
pSize = (mwSize *)mxGetPr(prhs[4]);
nDimNum = mxGetNumberOfDimensions(prhs[0]);
plhs[0] = mxCreateLogicalArray(nDimNum, pSize);
pD = (bool *)mxGetData(plhs[0]);
for (i = 0; i < pSize[0]; i++) // NOT i=i !
{
for (j = 0; j < pSize[1]; j++)
{
for (k = 0; k < pSize[2]; k++)
{
pD[i+j*pSize[0]+k*(pSize[0]*pSize[1])] = buildSpaceGrid[ixx-1+i+(iyy-1+j)*pSize[0]+(izz-1+k)*(pSize[0]*pSize[1])];
}
}
}
}
I keep getting this wierd error when I try to compile it using mex command in Matlab.
error C2296: '*': illegal, left operand has type 'int *'
Can someone help me fix this problem.
I was also wondering if there is a better (effiient) way of improving the performance of this line of code.
Thank you so much for your help.

Best Answer

You are not going to get any performance boost for this in a mex routine. Since the m-code and the mex code both do deep data copying, and since this action represents the bulk of the work, the mex routine would not be expected to do any better than the m-code for performance. Where a mex routine can help you is if you could do the downstream processing inside your mex routine to avoid the deep data copy. You don't appear to have that situation.
What line has the error? And what is the complete error message?