1 /*=================================================================
2 * syntax: SPMX = SPARSIFY(MX, THRES)
4 * SPARSIFY - sparsify the input matrix, i.e. ignore the values
5 * of the matrix which are below a threshold
7 * Input: - MX: m-by-n matrix (sparse or full)
8 * - THRES: threshold value (double)
10 * Output: - SPMX: m-by-n sparse matrix only with values
11 * whose absolut value is above the given threshold
13 * Written by Mirko Visontai (10/24/2003)
14 *=================================================================*/
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
22 /* Declare variable */
23 int i,m,n,nzmax,newnnz,col,processed,passed;
24 int starting_row_index, current_row_index, stopping_row_index;
25 double *in_pr,*in_pi,*out_pr,*out_pi;
26 mwIndex *in_ir,*in_jc,*out_ir,*out_jc;
29 /* Check for proper number of input and output arguments */
30 if ((nlhs != 1) || (nrhs != 2)){
31 mexErrMsgTxt("usage: SPMX = SPARSIFY(MX, THRES).");
33 /* if matrix is complex threshold the norm of the numbers */
34 if (mxIsComplex(prhs[0])){
35 /* Check data type of input argument */
36 if (mxIsSparse(prhs[0])){
39 in_pr = mxGetPr(prhs[0]);
40 in_pi = mxGetPi(prhs[0]);
41 in_ir = mxGetIr(prhs[0]);
42 in_jc = mxGetJc(prhs[0]);
43 nzmax = mxGetNzmax(prhs[0]);
46 thres = mxGetScalar(prhs[1]);
48 /* Count new nonzeros */
50 for(i=0; i<nzmax; i++){
51 if (sqrt(in_pr[i]*in_pr[i] + in_pi[i]*in_pi[i])>thres) {newnnz++;}
56 plhs[0] = mxCreateSparse(m,n,newnnz,mxCOMPLEX);
58 mexErrMsgTxt("Could not allocate enough memory!\n");
59 out_pr = mxGetPr(plhs[0]);
60 out_pi = mxGetPr(plhs[0]);
61 out_ir = mxGetIr(plhs[0]);
62 out_jc = mxGetJc(plhs[0]);
65 for (col=0; col<n; col++){
66 starting_row_index = in_jc[col];
67 stopping_row_index = in_jc[col+1];
68 out_jc[col+1] = out_jc[col];
69 if (starting_row_index == stopping_row_index)
72 for (current_row_index = starting_row_index;
73 current_row_index < stopping_row_index;
74 current_row_index++) {
75 if (sqrt(in_pr[current_row_index]*in_pr[current_row_index] +
76 in_pi[current_row_index]*in_pi[current_row_index] ) > thres){
78 out_pr[passed]=in_pr[current_row_index];
79 out_pi[passed]=in_pi[current_row_index];
80 out_ir[passed]=in_ir[current_row_index];
81 out_jc[col+1] = out_jc[col+1]+1;
89 plhs[0] = mxCreateSparse(m,n,0,mxCOMPLEX);
92 else{ /* for full matrices */
94 in_pr = mxGetPr(prhs[0]);
95 in_pi = mxGetPr(prhs[0]);
98 thres = mxGetScalar(prhs[1]);
100 /* Count new nonzeros */
102 for(i=0; i<m*n; i++){
103 if (sqrt(in_pr[i]*in_pr[i] + in_pi[i]*in_pi[i])>thres) {newnnz++;}
108 plhs[0] = mxCreateSparse(m,n,newnnz,mxCOMPLEX);
110 mexErrMsgTxt("Could not allocate enough memory!\n");
111 out_pr = mxGetPr(plhs[0]);
112 out_pi = mxGetPi(plhs[0]);
113 out_ir = mxGetIr(plhs[0]);
114 out_jc = mxGetJc(plhs[0]);
118 for (col=0; col<n; col++){
119 out_jc[col+1] = out_jc[col];
120 for (current_row_index=0; current_row_index<m; current_row_index++){
121 if (sqrt(in_pr[current_row_index+m*col]*in_pr[current_row_index+m*col] +
122 in_pi[current_row_index+m*col]*in_pi[current_row_index+m*col]) > thres){
124 out_pr[passed]=in_pr[current_row_index+m*col];
125 out_ir[passed]=current_row_index;
126 out_jc[col+1] = out_jc[col+1]+1;
133 plhs[0] = mxCreateSparse(m,n,0,mxCOMPLEX);
138 /* Check data type of input argument */
139 if (mxIsSparse(prhs[0])){
142 in_pr = mxGetPr(prhs[0]);
143 in_ir = mxGetIr(prhs[0]);
144 in_jc = mxGetJc(prhs[0]);
145 nzmax = mxGetNzmax(prhs[0]);
148 thres = mxGetScalar(prhs[1]);
150 /* Count new nonzeros */
152 for(i=0; i<nzmax; i++){
153 if ((fabs(in_pr[i]))>thres) {newnnz++;}
158 plhs[0] = mxCreateSparse(m,n,newnnz,mxREAL);
160 mexErrMsgTxt("Could not allocate enough memory!\n");
161 out_pr = mxGetPr(plhs[0]);
162 out_ir = mxGetIr(plhs[0]);
163 out_jc = mxGetJc(plhs[0]);
166 for (col=0; col<n; col++){
167 starting_row_index = in_jc[col];
168 stopping_row_index = in_jc[col+1];
169 out_jc[col+1] = out_jc[col];
170 if (starting_row_index == stopping_row_index)
173 for (current_row_index = starting_row_index;
174 current_row_index < stopping_row_index;
175 current_row_index++) {
176 if (fabs(in_pr[current_row_index])>thres){
177 out_pr[passed]=in_pr[current_row_index];
178 out_ir[passed]=in_ir[current_row_index];
179 out_jc[col+1] = out_jc[col+1]+1;
187 plhs[0] = mxCreateSparse(m,n,0,mxREAL);
190 else{ /* for full matrices */
192 in_pr = mxGetPr(prhs[0]);
195 thres = mxGetScalar(prhs[1]);
197 /* Count new nonzeros */
199 for(i=0; i<m*n; i++){
200 if ((fabs(in_pr[i]))>thres) {newnnz++;}
205 plhs[0] = mxCreateSparse(m,n,newnnz,mxREAL);
207 mexErrMsgTxt("Could not allocate enough memory!\n");
208 out_pr = mxGetPr(plhs[0]);
209 out_ir = mxGetIr(plhs[0]);
210 out_jc = mxGetJc(plhs[0]);
214 for (col=0; col<n; col++){
215 out_jc[col+1] = out_jc[col];
216 for (current_row_index=0; current_row_index<m; current_row_index++){
217 if (fabs(in_pr[current_row_index+m*col])>thres){
218 out_pr[passed]=in_pr[current_row_index+m*col];
219 out_ir[passed]=current_row_index;
220 out_jc[col+1] = out_jc[col+1]+1;
227 plhs[0] = mxCreateSparse(m,n,0,mxREAL);