Printing Sparse Matrix in mex function in CCS format

6 次查看(过去 30 天)
Hi,
I am relatively new user of mex and matlab.I have to do some operations on sparse matrices,which also includes printing them in CCS format.Here is the code that I have written.
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double *A;
int *jc,*ir;
int m,n,size;
int i;
A = mxGetPr(prhs[0]);
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
jc = (int*) mxGetJc(prhs[0]);
ir = (int*) mxGetIr(prhs[0]);
size = mxGetNzmax(prhs[0]);
plhs[0] = mxCreateSparse(m,n,size,mxREAL);
mexPrintf("JC: ");
for(i = 0; i < m;i++)
{
mexPrintf("%d ",jc[i]);
}
mexPrintf("\n");
mexPrintf("IR:");
for(i = 0;i < size;i++)
{
mexPrintf("%d ",ir[i]);
}
mexPrintf("\n");
mexPrintf("Values:");
for(i = 0;i < size;i++)
{
mexPrintf("%g ",A[i]);
}
mexPrintf("\n");
}
But the output that I am getting is like this.
>> a = sprand(4,4,0.3)
a =
(1,1) 0.4898
(3,2) 0.6463
(4,2) 0.7094
(2,4) 0.4456
(4,4) 0.7547
>> com(a)
JC: 0 0 1 0
IR:0 0 2 0 3
Values:0.489764 0.646313 0.709365 0.445586 0.754687
ans =
All zero sparse: 4-by-4
>> full(a)
ans =
0.4898 0 0 0
0 0 0 0.4456
0 0.6463 0 0
0 0.7094 0 0.7547
Is this correct? since I dont see the row indices getting printed correctly.Further, is there any in built function to print the sparse matrices in CCS format?
Kindly reply.
Cheers.

采纳的回答

James Tursa
James Tursa 2012-5-15
I'm not sure what your real question is. Do you just want to print a sparse matrix from within a mex function? If so, you can use this function:
void spprint(const mxArray *mx)
{
mwSize n, nrow;
mwIndex *ir, *jc;
mwIndex j, x, y;
double *pr;
if( !mxIsSparse(mx) ) return;
n = mxGetN(mx);
pr = mxGetPr(mx);
ir = mxGetIr(mx);
jc = mxGetJc(mx);
for( y=0; y<n; y++ ) {
nrow = jc[y+1] - jc[y];
for( x=0; x<nrow; x++ ) {
mexPrintf(" (%d,%d) %g\n",(*ir++)+1,y+1,*pr++);
}
}
}
Incidentally, you should never recast function pointer return values like this:
jc = (int*) mxGetJc(prhs[0]);
ir = (int*) mxGetIr(prhs[0]);
This will not work if mwIndex is not an int. Best to just declare jc and ir as mwIndex pointers (not int pointers) and then don't cast the result of the functions and you will be fine.

更多回答(3 个)

Abhishek
Abhishek 2012-5-16
Hi,
Thanks a ton for your reply. However, I just want to print the matrix as its component arrays, that is IR JC and Val.
Is it possible to print them?

James Tursa
James Tursa 2012-5-16
To print out the raw array contents:
void spprintraw(const mxArray *mx)
{
mwSize n;
mwIndex *ir, *jc;
mwIndex j, N;
double *pr;
if( !mxIsSparse(mx) ) return;
n = mxGetN(mx);
pr = mxGetPr(mx);
ir = mxGetIr(mx);
jc = mxGetJc(mx);
N = jc[n];
mexPrintf("JC:\n");
for( j=0; j<=n; j++ ) {
mexPrintf("%d ",jc[j]);
}
mexPrintf("\n");
mexPrintf("IR:\n");
for( j=0; j<N; j++ ) {
mexPrintf("%d ",ir[j]);
}
mexPrintf("\n");
mexPrintf("VAL:\n");
for( j=0; j<N; j++ ) {
mexPrintf("%g ",pr[j]);
}
mexPrintf("\n");
}
Keep the following points in mind:
- mxGetNzmax is useless for this. mxGetNzmax returns the amount of memory that is allocated for the sparse arrays stuff, which is not the same as the number of non-zeros presently in the array. Use jc[mxGetN(prhs[0])] to get the actual number of non-zero values held in the array currently.
- All indexing is 0-based, not 1-based.
- The jc array is the size of number_or_columns+1. It is basically an accumulation array containing the number of non-zero elements in total for all of the previous columns (which is why the last value in the array is the total number of non-zero elements in the array currently). It does not contain column indexes.

Abhishek
Abhishek 2012-5-16
Thanks a lot.I already wrote something similar but your explanation on size of JC and other caveats for indexing have given me some insights of the CCS format. Thanks a ton again.
  1 个评论
James Tursa
James Tursa 2012-5-16
To put it explicitly:
jc[0] = 0
jc[1] = The number of non-zero elements stored for 1st column
jc[2] = The number of non-zero elements stored for 1st-2nd columns
jc[3] = The number of non-zero elements stored for 1st-3rd columns
:
etc.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by