NumPy for Matlab users
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 :
|type(func)||source(func) or func??(for python)||prints source of the function|
|a | | b||a or b||Logical OR operator|
|eps||np.spacing(1)||Distance between 1 and nearest floating point number|
|1 * i, 1 * j, 1i, 1j||1j||Complex Numbers|
|help(func)||info(func) or help(func) (for python)||Provides for the given func function|
|a && b||a and b||Logical AND operator|
For Linear Algebra :
|size(m)||shape(a) or a.shape(a)||Returns the size of matrix|
|[1 2 3; 7 8 9]||array([[1, 2, 3], [7, 8, 9]])||2×3 matrix|
|m(end)||m[-1]||Last element in 1xn matrix|
|m(3, 4)||m||Access element in 3rd row, 4th element|
|m(2 : )||m or m[1, : ]|
Entire second row of matrix, as matlab’s first element
is indexed at 1 and that of numpy is indexed at 0.
|m(1 : 5, : )||m[0 : 5] or m[ : 5]||returns first five rows of matrix m|
|m.’||m.transpose() or a.T||Transpose of matrix m|
|m’||m.conj().transpose() or m.conj().T||Conjugate transpose of matrix m|
|m.^3||m**3||Element wise exponent of matrix m.|
|m .* n||m *n||Element wise matrix multiply|
|m * n||m @ n||Matrix multiplication (algebric operation)|
|a ./ b||a / b||Element wise divide of matrix m and n.|
|find(m > 2)||nonzero(m > 2)||Find the indices whose elements are greater then 2|
|m( : ) = 1||m[ : ] = 1||Set all the elements of matrix m to 1|
|a = b||a = b.copy()|
Assign b into a directly in Matlab while numpy assigns
|zeros(2, 4)||zeros((2, 4))|
two dimensional array with 2 rows and 4 columns full
with all elements as 0 (zero).
|zeros(1, 3, 5)||zeros((1, 3, 5))||1x3x5 three dimensional array with all elements zero.|
|rand(2, 4)||randomn.rand(2, 4)||2×4 two dimensional array with random elements.|
|ones(2, 4)||ones((2, 4))|
2×4 two dimensional array with all elements floating
|eye(4)||eye(4)||returns 4×4 identity matrix|
|max(max(m))||max(m)||returns maximum element present in matrix m|
|a | b||logical_or(a, b)||element by element logical OR operation|
|a & b||logical_and(a, b)||element by element logical AND operation|
|bitor(a, b)||a | b||Bitwise logical OR operator of matrices a and b|
|bitand(a, b)||a & b||bitwise logical AND operator of matrices a and b|
|sort(m)||sort(m) or m.sort()||sort the matrix m|
|ifft(m)||ifft(m)||Fourier transform of matrix m.|
linalg.solve(a, b) if a is square matrix
else, linalg.lstsq(a, b)
|solve the equation a x = b, for x|
|inv(m)||linalg.inv(m)||returns inverse of square matrix m|
|pinv(m)||linalg.pinv(m)||returns pseudo inverse of matrix m|
|max(m, n)||maximum(m, n)|
returns max element from each pair by comparing
m and n element by element.
returns maximum element from each column of
|linspace(a, b, m)||linspace(a,, b, m)|
returns m equally spaced elements between a and b