idamnjanovic@15
|
1 /**************************************************************************
|
idamnjanovic@15
|
2 *
|
idamnjanovic@15
|
3 * File name: sprow.c
|
idamnjanovic@15
|
4 *
|
idamnjanovic@15
|
5 * Ron Rubinstein
|
idamnjanovic@15
|
6 * Computer Science Department
|
idamnjanovic@15
|
7 * Technion, Haifa 32000 Israel
|
idamnjanovic@15
|
8 * ronrubin@cs
|
idamnjanovic@15
|
9 *
|
idamnjanovic@15
|
10 * Last Updated: 24.8.2009
|
idamnjanovic@15
|
11 *
|
idamnjanovic@15
|
12 *************************************************************************/
|
idamnjanovic@15
|
13
|
idamnjanovic@15
|
14
|
idamnjanovic@15
|
15 #include "mex.h"
|
idamnjanovic@15
|
16 #include "mexutils.h"
|
idamnjanovic@15
|
17
|
idamnjanovic@15
|
18
|
idamnjanovic@15
|
19 /* Input Arguments */
|
idamnjanovic@15
|
20
|
idamnjanovic@15
|
21 #define A_IN prhs[0]
|
idamnjanovic@15
|
22 #define J_IN prhs[1]
|
idamnjanovic@15
|
23
|
idamnjanovic@15
|
24
|
idamnjanovic@15
|
25 /* Output Arguments */
|
idamnjanovic@15
|
26
|
idamnjanovic@15
|
27 #define X_OUT plhs[0]
|
idamnjanovic@15
|
28 #define ID_OUT plhs[1]
|
idamnjanovic@15
|
29
|
idamnjanovic@15
|
30
|
idamnjanovic@15
|
31 void mexFunction(int nlhs, mxArray *plhs[],
|
idamnjanovic@15
|
32 int nrhs, const mxArray*prhs[])
|
idamnjanovic@15
|
33
|
idamnjanovic@15
|
34 {
|
idamnjanovic@15
|
35 double *pr, *x, *id, rowid;
|
idamnjanovic@15
|
36 mwIndex *ir, *jc;
|
idamnjanovic@15
|
37 mwSize m, n;
|
idamnjanovic@15
|
38 mwIndex i, j, k, l, rowlen;
|
idamnjanovic@15
|
39
|
idamnjanovic@15
|
40 if (nrhs != 2) {
|
idamnjanovic@15
|
41 mexErrMsgTxt("GETSPROW requires two input arguments.");
|
idamnjanovic@15
|
42 } else if (nlhs > 2) {
|
idamnjanovic@15
|
43 mexErrMsgTxt("Too many output arguments.");
|
idamnjanovic@15
|
44 }
|
idamnjanovic@15
|
45
|
idamnjanovic@15
|
46 checkmatrix(A_IN, "GETSPROW", "A");
|
idamnjanovic@15
|
47 checksparse(A_IN, "GETSPROW", "A");
|
idamnjanovic@15
|
48 checkscalar(J_IN, "GETSPROW", "J");
|
idamnjanovic@15
|
49
|
idamnjanovic@15
|
50 m = mxGetM(A_IN);
|
idamnjanovic@15
|
51 n = mxGetN(A_IN);
|
idamnjanovic@15
|
52
|
idamnjanovic@15
|
53 rowid = mxGetScalar(J_IN);
|
idamnjanovic@15
|
54 if (rowid < 0) {
|
idamnjanovic@15
|
55 mexErrMsgTxt("Invalid row index.");
|
idamnjanovic@15
|
56 }
|
idamnjanovic@15
|
57 j = (mwIndex)(rowid + 1e-2);
|
idamnjanovic@15
|
58 if (j<1 || j>m) {
|
idamnjanovic@15
|
59 mexErrMsgTxt("Row index is out of range.");
|
idamnjanovic@15
|
60 }
|
idamnjanovic@15
|
61 j--;
|
idamnjanovic@15
|
62
|
idamnjanovic@15
|
63 pr = mxGetPr(A_IN);
|
idamnjanovic@15
|
64 ir = mxGetIr(A_IN);
|
idamnjanovic@15
|
65 jc = mxGetJc(A_IN);
|
idamnjanovic@15
|
66
|
idamnjanovic@15
|
67 /* Determine length of row */
|
idamnjanovic@15
|
68 rowlen = 0;
|
idamnjanovic@15
|
69 for (i=0; i<jc[n]; ++i) {
|
idamnjanovic@15
|
70 rowlen += (ir[i]==j);
|
idamnjanovic@15
|
71 }
|
idamnjanovic@15
|
72
|
idamnjanovic@15
|
73 /* Allocate output parameters */
|
idamnjanovic@15
|
74 X_OUT = mxCreateDoubleMatrix(1, rowlen, mxREAL);
|
idamnjanovic@15
|
75 ID_OUT = mxCreateDoubleMatrix(1, rowlen, mxREAL);
|
idamnjanovic@15
|
76
|
idamnjanovic@15
|
77 x = mxGetPr(X_OUT);
|
idamnjanovic@15
|
78 id = mxGetPr(ID_OUT);
|
idamnjanovic@15
|
79
|
idamnjanovic@15
|
80 /* Compute j-th row */
|
idamnjanovic@15
|
81 k=0;
|
idamnjanovic@15
|
82 for (l=1; l<=n; ++l) {
|
idamnjanovic@15
|
83 i = jc[l-1];
|
idamnjanovic@15
|
84 while (i<jc[l] && ir[i]<j) {
|
idamnjanovic@15
|
85 i++;
|
idamnjanovic@15
|
86 }
|
idamnjanovic@15
|
87 if (i<jc[l] && ir[i]==j) {
|
idamnjanovic@15
|
88 x[k] = pr[i];
|
idamnjanovic@15
|
89 id[k] = l;
|
idamnjanovic@15
|
90 k++;
|
idamnjanovic@15
|
91 }
|
idamnjanovic@15
|
92 }
|
idamnjanovic@15
|
93
|
idamnjanovic@15
|
94 }
|