let's calculation , matrix of size 3 3 each time in loop. assume each time, want save such matrix in column of bigger matrix, number of rows equal 9 (total number of elements in smaller matrix). first reshape smaller matrix , try save 1 column of big matrix. simple code 1 column looks this:
import numpy 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 following error:
big[:,0]= np.reshape(small,(9,1)) valueerror: not broadcast input array shape (9,1) shape (9)
i tried use flatten, didn't work either. there way create shape(9) array small matrix or other way handle error?
your appreciated!
try:
import numpy np big = np.zeros((9,3)) small = np.random.rand(3,3) big[:,0]= np.reshape(small,(9,)) print big or:
import numpy np big = np.zeros((9,3)) small = np.random.rand(3,3) big[:,0]= small.reshape((9,1)) print big or:
import numpy np big = np.zeros((9,3)) small = np.random.rand(3,3) big[:,[0]]= np.reshape(small,(9,1)) print big either case gets me:
[[ 0.81527817 0. 0. ] [ 0.4018887 0. 0. ] [ 0.55423212 0. 0. ] [ 0.18543227 0. 0. ] [ 0.3069444 0. 0. ] [ 0.72315677 0. 0. ] [ 0.81592963 0. 0. ] [ 0.63026719 0. 0. ] [ 0.22529578 0. 0. ]] explanation
the shape of big trying assign (9, ) one-dimensional. shape trying assign (9, 1) two-dimensional. need reconcile making two-dim one-dim np.reshape(small, (9,1)) np.reshape(small, (9,)). or, make one-dim two-dim big[:, 0] big[:, [0]]. exception when assigned 'big[:, 0] = small.reshape((9,1))`. in case, numpy must checking.