Search code examples
pythonmatlabmatrixstore

Storing Multiple Matrices from a For Loop in Python


I am trying to convert the following Matlab code to Python:

  n = 10 ;
  T = cell(1, n) ;
  for k = 1 : n
    T{1,k} = 20*k + rand(10) ;
  end

It stored all the matrices generated from the for loop. How can I write a similar code in Python?


Solution

  • You can use a normal list:

    import numpy as np
    n = 10
    t = []
    for k in range(n):
      t.append(20 * (k+1) + np.random.rand(n,n))
    print(t)