在Matlab中表示矩阵(m,n)等价矩阵(:),冒号,在Python中

ppcbkaq5  于 2023-01-21  发布在  Matlab
关注(0)|答案(2)|浏览(162)

我需要在matlab中找到Python中A(:)的等价物,其中A是矩阵(m,n):
例如:

A =

 5     6     7
 8     9    10

A(:)
ANS =

5
 8
 6
 9
 7
10

先谢了!

xwbd5t1u

xwbd5t1u1#

如果要得到以列为主的结果(以符合Matlab约定),可能需要使用numpy矩阵的转置,然后使用ndarray.ravel()方法:

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

其给出:

array([ 5,  8,  6,  9,  7, 10])
hrysbysz

hrysbysz2#

您可以通过使用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'))

相关问题