How to translate matlab code into python (numpy)
Just like written languages, different programming languages have different innate styles, which suit different needs. Like Matlab love matrix, love script; python favors tensor, favors subfunction, favors objective-oriented programming; Mathematica are muchly functional; R are better at datatable demonstration.
But if we just want to translate an algorithm from one language to another, we can do so.
Note: An official table of translation could be looked up here. numpy for matlab users .
Objective : If we have a script in matlab, how can we translate that into python. (numpy and matplotlib)
- General
- Replace all the comment mark
%with#. use''''''for multiline. - Discard all
endand corret the indentation by tab - Line break
...in mat ,\in python lambdais protected in python, use another var name
- Replace all the comment mark
- Operators
^in matlab =**in python.*./.^in matlab = default operator innumpy- Matrix transpose
'into.T - Matrix multiplication
*in matlab,.in Mathematica, but not easy in python. Seems for 2ndarray,A@Bdo the job in numpy!
Indexing
- Matlab indexing using
( , ), python using[ , ]. Mathematica using[[, ]]
Matlab start from
1end inend, numpy from0to ``(nothing)Note one of the super useful thing in matlab is
findi.e. find all the none zero entries’ index. Python doesn’t have function with exactly the same functionality.np.non_zero,np.wheremay be a choice for numpy arrays, but the indices it returns are more complex, you need to extract it from tuple.ii = np.where(values == searchval)[0][i for i, x in enumerate(my_list) if x == "whatever"]may be a quick way to do so for lists.
- Matlab indexing using
Number Array
- Matlab use
strt:step:endnumpy userange(strt,end,step) - And use
np.arangefor actual array. np.arrayhas a little bit complex control of dimension: all 1-d array (even vertical ones) are neither 1n nor n1 thus has to be reshaped to do matrix multiply!size(),length()in matlab,len()in python[;]can represent break the row in matlab, but cannot do so in python.- Append with
+or[].append()in Python;list = [list, new_itm]in Matlab
- Matlab use
Numpy functions
minin matlab works element wise, the corresponding function innumpyisminimumnotminnumpyis more sensitive to data type, sometimes you don’t need to change data type tointto run in matlab. But you have to cast tointto run in python. Esp. when doing indexing!
- Argument format is quite different for quite a few common functions
np.zerosonly takes list or tuple of size. if only input one valuenp.zeros(3)it outputs a 1d arrayzerostakes both list and individual values, if only input one valuezeros(3)it outputs a square mat
normin matlab doesn’t work for vector norm along 1 axis.np.linalg.normcan do this.
Dictionary and Struct and Cell
- In matlab, no
dictto use!structis similar, you can add fields to astructobject structis also good for structured array data!repmat(struct('image',[],'eig',[]),1,10)can create an array of structure which is easy to- Matlab
cellis like the pythonlistwhich is a universal container for any heterogenous things.
- In matlab, no
String and Printing
- Python string is so easy to use
"%s-%d" % (str,int)syntax can perform string formatting anywhere; Matlab has more cumbersome syntax, with several choicessprintf(%s-%d", (str,int))compose(%s-%d", (str,int))
- Python print function
printis so flexible. Matlab counterpart isdispbut more for display array and structure instead of string - Python
print(str)will implicitly add\nto the end to change line. For Matlab,fprintfwill not add\nyou have to add yourself. Butdispwill change line automatically
- Python string is so easy to use
Flow Control
for i=1:2:10tofor i in range(1,10,2):- Note Python add
:to any control command; Matlab don’t
Common command
- Print out words: In Python,
print("%.f, %.d".%( x, arg2)); In matlabfprintf("x: %.1f, arg1: %d", x, arg2). Or usingdisp(var) - Formatted string control: In python,
"%.f, %.d".%( x, arg2), in matlabstr = sprintf("x: %.1f, arg1: %d", x, arg2)
- Print out words: In Python,
Subfunction define
- in matlab
function out=Y(in)
....
end
in python
def Y(param):
return out
Tricky points
- Complicated control of
ndarrayandmatrixin numpy- Reduce into 1D:
M.flatreturn iterator,M.flatten()return a 1_D array - In matlab
A(:)will give you a 1d array, but in pythonA[:]will return the same shape. - Aside from reshape there are quite a few handy functions in matlab See this.
- Reduce into 1D:
- Array Storage in Memory
- A subtle difference is the convention of storing multidimensional array in matlab or python. As matlab use column major order, and numpy and torch use row major order. Major order
- Note row major is the convention of C, Pascal etc. Column major is the convention of Fortran. (Matlab and Julia inherit this) So in python you can specify these 2 conventions by
Cfor row major andFfor column major.output=output.reshape((row*col, depth), order='F')(Really confusing….) - Because of this, the 2 language can have very different result when applying
reshapeoperation. If you really want to match the result from 2 languages, carefully match your input and then:- See which numbers come consecutively on the first dimension.
- And then permute those axis to left most and then do reshape.
- For python, you should look at the right most dimension, entries will be stored in that dimension first.
- See the following corresponding code in matlab vs python
def pixelshuffle(inputs, upscale_factor=3):
batch_size, channels, in_height, in_width = inputs.size()
channels //= upscale_factor ** 2 #*3 if 3D
#out_depth = in_depth * self.upscale_factor if 3D
out_height = in_height * upscale_factor
out_width = in_width * upscale_factor
input_view = inputs.contiguous().view(
batch_size, channels, upscale_factor, upscale_factor, in_height, in_width)
#shuffle_out = input_view.permute(0, 1, 5, 2, 6, 3, 7, 4).contiguous() if 3D
shuffle_out = input_view.permute(0, 1, 4, 2, 5, 3).contiguous()
return shuffle_out.view(batch_size, channels, out_height, out_width)
Corresponds to matlab code
function [out] = pixelshuffle(input, upscale)
if nargin == 1
upscale = 2;
end
H = size(input,1); W = size(input,2);
Ch = size(input,3) / upscale.^2;
B = size(input,4);
input_fact = reshape(input,[H,W,upscale,upscale,Ch,B]);
out = reshape(permute(input_fact, [4,1,3,2,5,6]), [H * upscale,W * upscale,Ch,B]);
end
Let Matlab and Python Talk to Each Other
- For common non-binary file types, they usually have good readers like
csv, pythonimport csvjson- Which are good for exchange of configurations and string information
.matformat could be read in python, with some drawbacks- Old simple
.matfiles could be read byscipy.io.loadmat - new
.matfiles (v7.3+) could be read byh5py.File- The matrix and tensor will be read directly as datasets
- But the objects will be formed in a reference array, and you feed the reference back to the file to get the object into python. See Refs in h5py.
- Old simple
.npyformat could be read in matlab
For more detailed exposition on this topic, see the other notes Data Transport between Python Matlab, Matlab Python Hybrid Programming