简体   繁体   中英

How can I make a Numpy matrix with values in a certain range?

I know if I do m = np.arange(1, 10), I'll get a matrix with values from 1 to 9 going across the rows. Is there a similar function to do this with values from 1 to 9 that go down the columns? ie,

1 4 7
2 5 8
3 6 9

You can use the reshape function from numpy

np.arange(1, 10).reshape(3,3,order='F')

array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

numpy reshape function can help

m.reshape(3,3, order='F')

One of the ways to make your matrix column major is using reshape.

np.reshape(m, (3, 3), order='F')

More informations: https://numpy.org/doc/stable/reference/generated/numpy.reshape.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM