开发者

Mex sparse matrix

开发者 https://www.devze.com 2023-03-08 01:06 出处:网络
I created a sparse matrix in MEX using mxCreateSparse. mxArray *W; W=mxCreateSparse(n*n,n*n,xsize,mxREAL);开发者_运维百科

I created a sparse matrix in MEX using mxCreateSparse.

mxArray *W;
W=mxCreateSparse(n*n,n*n,xsize,mxREAL);开发者_运维百科
double *wpoint;
wpoint=mxGetPr(W);
for(p=0;p<xsize;p++)
 {
     Wpoint[(returnindex1(xi[p][0],xi[p][1])-1)*n*n + (returnindex1(xj[p][0],xj[p][1]))]=   exp(-df[p]/(SIGMAI*SIGMAI)) * exp(-dx[p]/(SIGMAJ*SIGMAJ));
 }

the maximum value which comes from (returnindex1(xi[p][0],xi[p][1])-1)*n*n + (returnindex1(xj[p][0],xj[p][1])) is n*n*n*n and I have created the sparse matrix of dimension (n*n)X(n*n)

When I display the whole matrix, some of the zero elements come as junk. Also for large values of n, segmentation fault occurs at wpoint.


The pr array holds xsize elements and you accessing the array with out of bounds indices . Hence the seg violation.

I think your fundamental problem is that you have not fully grasped how sparse matrices are stored in MATLAB. I'm not an expert on the MATLAB implementation details but my recollection is that it uses compressed column storage.

In essence there are 3 arrays as follows:

  • double pr[NZMAX] which contains the NZMAX non-zero values.
  • int ir[NZMAX] which contains the row number of each value in pr.
  • int jc[m] which indexes into pr and ir identifying the first item in each of the m columns.

That's the executive summary, but I recommend that you read up on the details more carefully.

0

精彩评论

暂无评论...
验证码 换一张
取 消