In Matlab for a matrix(m,n) equivalent matrix(:) , colon, in python

Question:

I need to find the equivalent from matlab of A(:) in python. Where A is a matrix(m,n):

For example:

A =

 5     6     7
 8     9    10

A(:)

ans =

 5
 8
 6
 9
 7
10

thanks in advance!

Asked By: Nikko

||

Answers:

You can do this by reshaping your array with numpy.reshape

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.reshape.html

import numpy
m = numpy.array([[ 5, 6, 7 ], [ 8, 9, 10 ]])
print(numpy.reshape(m, -1, 'F'))
Answered By: S.MC.

If you want the column-major result (to match the Matlab convention), you probably want to use the transpose of your numpy matrix, and then the ndarray.ravel() method:

m = numpy.array([[ 5, 6, 7 ], [ 8, 9, 10 ]])
m.T.ravel()

which gives:

array([ 5,  8,  6,  9,  7, 10])
Answered By: rwp
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.