From: James Tursa on
"regujr regujr" <kiregujr(a)yahoo.it> wrote in message <i34sfj$9c6$1(a)fred.mathworks.com>...
> > 1 million is large but not huge. That's only about 8MB per row, column, and value vector. MATLAB can easily handle this and there should be no memory problem. For these sizes, my advice is to just pass them back to MATLAB and use the sparse command to build your sparse matrix on the MATLAB side. You could also do this inside the mex routine but it won't really save you much, if anything.
> >
> > James Tursa
>
> Your observation is right but I can have 10 mld of elements in a vector ^_^. I must work with image and sometimes I can have HD images ^^. Otherwise I'm not here to ask help ^^. I try to understand how matlab implement the function "Sparse" but I don't find nothing. I need only that part of code, in fact I convert all the rest of the program, but I can not reproduce the method that put data in sparse matrix starting from row, col and vals vectors ^^
>
> If you can tell me where I can find, or how I can reimplement code (without using sort algoritm to re-organizate vectors) I will appreciate.
>
> Thk in advance

Will something like this work for you? If not, then why not?

James Tursa

//-----------------------------------------------------------------------------------------------
// No input. Returns a 1 million x 1 million sparse diagonal matrix.
//
#include "mex.h"
#define N 1000000
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *row, *col, *val;
mwSize i;
mxArray *rhs[3];

rhs[0] = mxCreateDoubleMatrix(1, N, mxREAL);
rhs[1] = mxCreateDoubleMatrix(1, N, mxREAL);
rhs[2] = mxCreateDoubleMatrix(1, N, mxREAL);
row = mxGetPr(rhs[0]);
col = mxGetPr(rhs[1]);
val = mxGetPr(rhs[2]);
for( i=0; i<N; i++ ) {
row[i] = col[i] = val[i] = i+1;
}
mexCallMATLAB(1, plhs, 3, rhs, "sparse");
mxDestroyArray(rhs[0]);
mxDestroyArray(rhs[1]);
mxDestroyArray(rhs[2]);
}
From: Steven_Lord on


"regujr regujr" <kiregujr(a)yahoo.it> wrote in message
news:i33696$ihp$1(a)fred.mathworks.com...
> Hi,
> I need help to resolve a problem. I do a code in C that I use as mex and
> I want create a sparse matrix and this is must return in matlab.
> I have 3 vectors:
> - row vector
> - column vector
> - value vector
> Now I return this 3 vectors and in matlab I use command "sparse" to create
> the matrix. But as you can understand, if I return 3 vectors sometimes I
> have problem of "Out of memory".
> I try to use mxCreateSparse, but I understand that I can not pass these 3
> vectors and only solution is do an order of these array but this is too
> long!

No matter how you try to create the sparse matrix, you (or MATLAB) will need
to sort the row/column/value vectors at some point.

> I see that matlab when I use "sparse" take about 10 seconds and I want
> only reproduce this performance in C - mex code.

Then use mxCreateSparse, mxGetPr, mxGetIr, mxGetJc, and the corresponding
mxSet* functions along with a GOOD sorting code.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com