Matlab → Python only 1xN vectors

Matlab ≥ R2014b calling Python can only send a 1xN vector. You have to reshape the matrix into a 1xN vector when passing the matrix into Python, and reshape back to a matrix inside Python, involving memory copies at both Matlab reshapings.

Here is an example of calling Python from Matlab for image processing

Note that Numpy ndarray is not understandable by Matlab, so you will have to make your Numpy array into a 1-D list and then send it back. That’s a lot of memory copying!

Works

I can pass

py.numpy.sqrt(2)

ans=1.4142

py.numpy.sqrt([2,2])

ans=[ 1.41421356  1.41421356]

Does not work

py.numpy.sqrt([2,2;2,2]) 

Error using py.numpy.sqrt Conversion of MATLAB ‘double’ to Python is only supported for 1-N vectors.

Notes

Mathworks confirms: cannot pass normal 2D matrices to Python from Matlab

Leave a Comment