Nov-11-2019, 09:03 AM

Hi

As described in the following example, I want to introduce a number in a matrix name (number is a variable). I had a look on internet and I found a way but I'm wondering if there's something better than using "locals"?

Paul

As described in the following example, I want to introduce a number in a matrix name (number is a variable). I had a look on internet and I found a way but I'm wondering if there's something better than using "locals"?

import numpy as np mat = np.zeros((2,3,3)) mat[0,:,:] = np.eye(3) mat[1,:,:] = 2*np.eye(3) print(mat) for i in range(1,4): my_var = 'mat{}'.format(i) locals()[my_var] = i*mat print("mat{} = {}".format(i, locals()[my_var]))Thanks

Paul