Let's say I do some calculation and I get a matrix of size 3 by 3 each time in a loop. Assume that each time, I want to save such matrix in a column of a bigger matrix, whose number of rows is equal to 9 (total number of elements in the smaller matrix). first I reshape the smaller matrix and then try to save it into one column of the big matrix. A simple code for only one column looks something like this:
import numpy as np
Big = np.zeros((9,3))
Small = np.random.rand(3,3)
Big[:,0]= np.reshape(Small,(9,1))
print Big
But python throws me the following error:
Big[:,0]= np.reshape(Small,(9,1)) ValueError: could not broadcast input array from shape (9,1) into shape (9)
I also tried to use flatten, but that didn't work either. Is there any way to create a shape(9) array from the small matrix or any other way to handle this error?
Your help is greatly appreciated!
Big[:,0]= np.reshape(Small,(9))Big[:,0]doesslicingand therefore expects a1Darray, whereasnp.reshape(Small,(9,1))is a2D. So, that's why as suggested by @C_Z_ you could reshape to1Darray like that or flatten to1Dwith.ravel(). So, in summary, you could do :Big[:,0]= Small.ravel().