Printing Sparse Matrix in mex function in CCS format
Show older comments
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.
Accepted Answer
More Answers (3)
Abhishek
on 16 May 2012
0 votes
James Tursa
on 16 May 2012
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
on 16 May 2012
0 votes
1 Comment
James Tursa
on 16 May 2012
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!