Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python capable of doing MATLAB equivalent matrix operations?

I have implemented codes in MATLAB that operates on 216x216 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.

like image 341
Dexters Avatar asked Mar 15 '26 16:03

Dexters


1 Answers

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.)

like image 143
Steve Tjoa Avatar answered Mar 17 '26 04:03

Steve Tjoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!