0

I'm using a mex file in matlab to find a fast way to find a row matrix s in a large matrix S. The mexfile written down below does that, but only for a unique s in S. However the row matrix s appears a number of times in large matrix S in my application.

    #include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[], mwSize, lastfound)
{
double *S, *s *lastfound;
mwSize n1, n2, i1, i2, k;
  S  = mxGetPr(prhs[0]);
  n1 = mxGetM(prhs[0]);
  // n2 = mxGetN(prhs[0]);
  n2 = 3;
  s  = mxGetPr(prhs[1]);  
  for (i1 = lastfound; i1 < n1; i1++) {
     k = i1;
     for (i2 = 0; i2 < n2; i2++) {
        if (S[k] != s[i2]) {
           break;
        }
        k += n1;
     }
     if (i2 == n2) {  // Matching row found:
        plhs[0] = mxCreateDoubleScalar((double) (i1 + 1));
        return;
     }
  }
  // No success:
  plhs[0] = mxCreateDoubleScalar(mxGetNaN());
}

So i tried to use another input 'lastfound' which would enable the code to start at lastfound + 1 (the +1 is added when the function is called), so that when the previous row s is found, and the code start searching the second row s. I do this until the mex file return NaN. It however doesn't work, the code doesn't start the at the lastfound + 1 step. I am a C noob, so i think it is a very small error, but i would immensely appreciate it if somebody could take a look at it.

Thanks in advance :)

Kind regards,

Mark

  • I have not used mex, but there are some issues that I am fairly sure needs to be fixed after a short look at the code: 1) The mex function is supposed to have 4 arguments `void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])` this function should be declared in mex.h. For more info, see matlab help 2) Even if more than 4 arguments was allowed they must be declared first. `mexFunction( ..., mwSize` is not allowed. This gives an undeclared variable error. **Variables must be declared before the are used** – patrik Mar 16 '15 at 09:21
  • Sorry, I missed the link [mexFunction help](http://se.mathworks.com/help/matlab/apiref/mexfunction.html). However, what is the problem with normal subreferencing in matlab? – patrik Mar 16 '15 at 11:43
  • Hi, thanks for your anwsers! What do you mean with normal subreferencing in matlab? – Mark Roelofs Mar 16 '15 at 14:32
  • Ok I see. You mean do a row search? Sorry did not get that at first. What is the size of the matrix? – patrik Mar 16 '15 at 15:52
  • You may actually be able to do what you want using `bsxfun` or `arrayfun`. These are matlab builtins so they should be fairly fast. – patrik Mar 16 '15 at 16:14
  • You may want to look at this post, which provides vectorized solutions to a similar problem, written in matlab. http://stackoverflow.com/questions/6209904/find-given-row-in-a-matrix – patrik Mar 17 '15 at 07:07
  • My big matrix S is extremely big. Maybe like 1e7 rows. I tried all the links and function you posted before and they aren't faster than the C mex file. Mex file is about 3 times faster than bsxfun and since running the programma takes hours I would be happy to use the mex file. – Mark Roelofs Mar 18 '15 at 08:44

0 Answers0