0
votes

I'm trying to take a matrix from c++ and import it to Matlab to run bintprog on this matrix, call it m. My c++ code generates these matrices of a certain type, and I need to run bintprog on them quickly, and with ideally millions of matrices.

So any of the following would be great: A way to import a bunch of matrices at once so I can run a lot of iterations thru my Matlab code. Or If I could implement Matlab code right in c++ nicely.

If this is not clear leave me comments and I'll update what I can.

2
You could just call Matlab from C++ -- mathworks.co.uk/help/matlab/…High Performance Mark

2 Answers

1
votes

You can call Matlab commands from C++ code (and vice versa):

  1. Compile your C++ code into a mex function and call bintprog using mexCallMatlab.

  2. As proposed by Mark, you may call Matlab engine from native C++ code using matlab engine.

  3. You may compile your C++ code as a shared library and call it from Matlab using calllib.

1
votes

I suggest the simple solution, assuming that your matrices are kept in 3-dimmensional array:

Build a loop in C++, to save your matrices... Something like this:

ofstream arquivoOut0("myMatrices.dat");
  for(int m=0;m<numberMatrices;m++){ 
      for (int i=0; i< numberlines;i++){
           for(int j=0;j<numberColumns;j++)
               if(j!=numberColumns-1) arquivoOut0<< matrices[m][i][j] << "\t";
               else arquivoOut0<< matrices[m][i][j] << "\n";
           }
       }
  }
arquivoOut0.close();

Ok. You have saved your matrices in an ascii file! Now you have to read it in Matlab!

load myMatrices.dat

  for m=1:numberMatrices
      for i=1:numberLines
           for j=1:numberColumns
               myMatricesInMatlab(m,i,j)=myMatrices((m-1)*numberLines+i,j);
           end
       end
  end

Now, you can use the toolbox that you need:

for i=1:numberMatrices
    Apply the toolbox for myMatricesInMatlab(i,:,:);
end

I think it works, it the processing time is not an issue!