Related Articles

NumPy for Matlab users

• Last Updated : 21 Aug, 2020

NumPy and Matlab are very similar to each other, both are made for mathematical and scientific calculations.  But still there are many differences, numPy is made to do scientific calculations with python using arrays while Matlab uses matrices. This article will give us a deep idea about similarities and differences between NumPy and Matlab and how can a Matlab user become proficient in NumPy.

NumPy uses both arrays and matrices. NumPy provides a special type of matrix, numpy.matrix and it is a subclass of ndarray (that is n dimensional array). The basic datatype of NumPy is n dimensional arrays in which operations are performed element wise. one has to use specific functions to perform linear algebra operations in  NumPy. While the basic datatype of Matlab is multidimensional array of double precision floating point numbers. The operations on instances of such data types is performed like matrices in linear algebra, most of the in built functions in Matlab takes and returns such type of arrays. NumPy was designed for python which is a general purpose programming language and it uses 0 (zero) based indexing. While Matlab’s scripting language is created for doing linear algebra operations but the syntax of Matlab for some array manipulation is more complex then NumPy’s. Also Matlab uses 1 (one) based indexing, means first element of the array is indexed at 1.

From this basic talk, the question arises which should we use arrays or matrix?

NumPy provides both array class and matrix class, array class provides basic n dimensional array computations while matrix class provides linear algebra computations specifically. Some of the key differences between array objects and matrix objects are :

• Handling of high dimensional arrays :-
• Array objects can have more then two dimensions while
• Matrix objects have only two dimensions.
• Handling of vectors (one dimensional arrays) :-
• In array objects, one dimensional arrays will always have single dimension. For arrays, N*1 and 1*N are two dimensional arrays not single dimension. Array operations on one dimensional array will always return 1-D array itself.
• While in matrix the single dimension array of size N is treated as and by default converted into N*1 or 1*N matrix. Any operation on it will always return a two dimensional array.
• Operators ‘*’ and ‘@’ :-
• In arrays, ‘ * ‘ operator is used for element wise multiplications or using the function multiply() and ‘ @ ‘ is used for matrix multiplication or using the function dot().
• In matrix, matrix multiplication is done using ‘ * ‘ operator, and for element wise multiplication have to use multiply() function.
• Constructor :-
• In arrays, constructors initializes with python sequences. eg- array([[1, 2, 3], [4, 5, 6]])
• In matrix, constructor takes string as the initializer. eg- matrix(“[1 2 3; 4 5 6]”)

Some common Matlab equivalents for NumPy :

For General Purpose :

For Linear Algebra :

My Personal Notes arrow_drop_up