[Solved] Dynamic Matrix in Python? [closed]


If you have huge vectors/matrixes use numpy anyway !

If you know the dimensions in advance, you can do:

nrow, ncol= 4,10
M0 = np.zeros((nrow,ncol))

vx = np.arange(nrow) + 10
vy = np.arange(ncol) + 10

M0[2,:] = vy
M0[:,5] +=  vx
M0

array([[ 0.,  0.,  0.,  0.,  0., 10.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0., 11.,  0.,  0.,  0.,  0.],
       [10., 11., 12., 13., 14., 27., 16., 17., 18., 19.],
       [ 0.,  0.,  0.,  0.,  0., 13.,  0.,  0.,  0.,  0.]])

If you do not know the dimensions in advance, you can check them, once you have some vectors as vx or vy:

nx = vx.shape[0]
ny = vy.shape[0]
nx,ny

(4, 10)

If you realy want to start with an empty matrix, you can do:

M1 = np.array([])
M1 = np.hstack((M1,vy))
M1

array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19.])

or:

M2 = np.r_['c',vx]
M2

matrix([[10],
        [11],
        [12],
        [13]])

Perhaps you should be more precise what type of dynamics on the matrix is needed and – as @jonrsharpe mentioned – wether you have sparse data in the matrix.

solved Dynamic Matrix in Python? [closed]