Reputation: 219
I have a 2x4 matrix of the form:
m = [[1,2,3,4],
[5,6,7,8]]
an I'm trying to get an 8x1 vector of the form:
m_new = [1,5,2,6,3,7,4,8]
Would the reshape() function be able to do something like this? Using
m_new = m.reshape(8,1)
unfortunately doesn't seem to do the job, but hopefully there are other settings in reshape that can achieve this.
Upvotes: 1
Views: 188
Reputation: 51165
You are looking for the order
parameter, which can be applied to almost any method numpy
uses to flatten multidimensional arrays.
order : {‘C’, ‘F’, ‘A’}, optional
- ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest.
Setup
m = np.arange(1, 9).reshape(-2, 4)
Examples:
m.reshape(8, order='F')
m.flatten(order='F')
m.ravel(order='F')
All result in:
array([1, 5, 2, 6, 3, 7, 4, 8])
Upvotes: 2