Is python capable of doing MATLAB equivalent matrix operations?

Question:

I have implemented codes in MATLAB that operates on 216×216 matrices that contain numeric data and sometime strings. The operations that I do on these matrices are mostly like filter matrices above a certain threshold, find all the matrix indexes that are above some value, Find a list of values above say X and then find consecutive differences between them, some string replace manipulations. Do matrix dot products etc. I need to access thousands of files to generate these matrices(dlmread I use in MATLAB).

Now I am in need to implement the above project in any other language that are usually bundled with an OS say Perl, c or python or opensource language.

I did a brief search and found out that python is a good tool for research. Does python has some of these MATLAB equivalents for matrix operations ( like read a file directly into an array, find, dlmwrite etc )

Because my codes already have a lot of loops without these MATLAB functions the codes would get much messier and difficult to maintain.

Or could you point out any other alternatives. I am familiar with little Perl but not python or R.

Asked By: Dexters

||

Answers:

You may want to take a look at NumPy / SciPy, they may help you do what you want. Plus there are a sizable number of users which will make it easier to get help when needed. General Intro to both libraries here

Answered By: Levon

Start with this page comparing NumPy and Matlab.

Here are some examples regarding your post:

In [5]: import scipy

In [6]: X = scipy.randn(3,3)

In [7]: X
Out[7]: 
array([[-1.16525755,  0.04875437, -0.91006082],
       [ 0.00703527,  0.21585977,  0.75102583],
       [ 1.12739755,  1.12907917, -2.02611163]])

In [8]: X>0
Out[8]: 
array([[False,  True, False],
       [ True,  True,  True],
       [ True,  True, False]], dtype=bool)

In [9]: scipy.where(X>0)
Out[9]: (array([0, 1, 1, 1, 2, 2]), array([1, 0, 1, 2, 0, 1]))

In [10]: X[X>0] = 99

In [11]: X
Out[11]: 
array([[ -1.16525755,  99.        ,  -0.91006082],
       [ 99.        ,  99.        ,  99.        ],
       [ 99.        ,  99.        ,  -2.02611163]])

In [12]: Y = scipy.randn(3,2)

In [13]: scipy.dot(X, Y)
Out[13]: 
array([[-124.41803568,  118.42995937],
       [-368.08354405,  199.67131528],
       [-190.13730231,  161.54715769]])

(Shameless plug: a comparison I once made between Python and Matlab.)

Answered By: Steve Tjoa