Reputation: 125
How can I make a matrix like mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]]) flatten to an array like arr0 = np.array([1,2,3,4,5,6,7,8,9])? Or perhaps a set?
mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr0 = np.array([1,2,3,4,5,6,7,8,9])
Upvotes: 0
Views: 381
Reputation: 369074
Use reshape:
reshape
>>> import numpy as np >>> mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]]) >>> mat0.reshape(-1) array([1, 2, 3, 4, 5, 6, 7, 8, 9])
or ravel:
ravel
>>> mat0.ravel() array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Upvotes: 1