comparison util/ksvd utils/sprow.c @ 70:c3eca463202d

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