octave

Numpy arrays and Matlab

I need to move some of the numpy and scipy matrices generated in python to matlab so that I can use the cvx package for optimization. I used the matrix market format to export data from python to matlab. To do so we need to do the following:

import scipy.io as sio
#A is the required matrix sparse or dense
sio.mmwrite(filename, A)
#note the extension .mtx is given to the filename by scipy

To work with matrix market format in matlab we need to have the files – mminfo.m, mmread.m, mmewrite.m all them can be found from the matrix market website. These files must be present either in the present directory in matlab or in the path directories. Suppose the file ‘Mat1.txt.mtx’ contained our matrix that we saved from python to read it in matlab we need to just write the following code.

A = mmread('Mat1.txt.mtx')

The required matrix will be stored as in variable A.

Advertisements