how to create a single float sparse matrix in mex files

非 Y 不嫁゛ 提交于 2019-12-02 06:00:46

问题


This Creating sparse matrix in MEX has a good example on mxCreateSparse. But this function return a double sparse matrix instead of single. If I want to return a single sparse matrix, what should I do ? Thanks !


回答1:


As @horchler suggested, you could use the undocumented function mxCreateSparseNumericMatrix. Example:

singlesparse.c

#include "mex.h"
#include <string.h>    /* memcpy */

/* undocumented function prototype */
EXTERN_C mxArray *mxCreateSparseNumericMatrix(mwSize m, mwSize n, 
    mwSize nzmax, mxClassID classid, mxComplexity ComplexFlag);

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    const float pr[] = {1.0, 7.0, 5.0, 3.0, 4.0, 2.0, 6.0};
    const mwIndex ir[] = {0, 2, 4, 2, 3, 0, 4};
    const mwIndex jc[] = {0, 3, 5, 5, 7};
    const mwSize nzmax = 10;
    const mwSize m = 5;
    const mwSize n = 4;

    plhs[0] = mxCreateSparseNumericMatrix(m, n, nzmax, mxSINGLE_CLASS, mxREAL);
    memcpy((void*)mxGetPr(plhs[0]), (const void*)pr, sizeof(pr));
    memcpy((void*)mxGetIr(plhs[0]), (const void*)ir, sizeof(ir));
    memcpy((void*)mxGetJc(plhs[0]), (const void*)jc, sizeof(jc));
}

Usage:

>> mex -largeArrayDims singlesparse.c

>> s = singlesparse()
s =
   (1,1)        1
   (3,1)        7
   (5,1)        5
   (3,2)        3
   (4,2)        4
   (1,4)        2
   (5,4)        6
>> ss = double(s);
>> whos s ss
  Name      Size            Bytes  Class     Attributes

  s         5x4               160  single    sparse    
  ss        5x4               152  double    sparse    

>> f = full(s)
One or more output arguments not assigned during call to "full". 
>> f = full(ss)
f =
     1     0     0     2
     0     0     0     0
     7     3     0     0
     0     4     0     0
     5     0     0     6

>> s + s;
Undefined function 'plus' for input arguments of type 'single' and attributes 'sparse 2d real'. 
>> ss + ss;
>> 2 * s;
Error using  * 
Undefined function 'times' for input arguments of type 'single' and attributes 'sparse 2d real'. 
>> 2 * ss;
>> s * s';
Error using  * 
MTIMES is not supported for one sparse input and one single input. 
>> ss * ss';

>> nnz(s)
ans =
     7
>> nzmax(s)
ans =
    10

>> dmperm(s)
Undefined function 'dmperm' for input arguments of type 'single'. 
>> dmperm(ss)
ans =
     1     3     0     5

>> svds(s)
Error using horzcat
The following error occurred converting from double to single:
Error using single
Attempt to convert to unimplemented sparse type
Error in svds (line 64)
B = [sparse(m,m) A; A' sparse(n,n)]; 
>> svds(ss)
ans =
    9.9249
    5.5807
    3.2176
    0.0000

>> % abs(s), cos(s), sin(s), s.^2, s.*s, etc.. all give errors

As you can see, the sparse single array was created successfully, however many functions expect the array to be of type double, so there is a lot of missing functionality...

Another restriction is that you cannot create multi-dimensional sparse arrays in MATLAB, they have to be 2D matrices..

Bottom line is: stick with double sparse 2D matrices in MATLAB!



来源:https://stackoverflow.com/questions/20552421/how-to-create-a-single-float-sparse-matrix-in-mex-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!