Home > Mobile >  Can anybody help me with my `for loops` using numpy vstack?
Can anybody help me with my `for loops` using numpy vstack?

Time:11-19

Say I have about 640 numpy arrays to stack vertically. Each array is of the size (66, 1). Doing this manually like this:

A = np.vstack((Ne['State_1_inc'], Ne['State_2_inc'], Ne['State_3_inc'], Ne['State_4_inc'], ..., Ne['State_640_inc']))

would obviously take a long time and is very time consuming. The end result would have A the size (66,640). Anybody know if I could do a for loop that will pass in all of my 640 states so I can build my matrix? New to programming here, thanks!

CodePudding user response:

Assuming you want to use all the elements of your dictionary:

Ne = {1: [1,2,3], 2: [4,5,6]}
np.vstack(list(Ne.values()))
# array([[1, 2, 3],
#        [4, 5, 6]])

Else you can use a dict comprehension:

np.vstack([Nef[f'State_{i 1}_inc'] for i in range(640)])
  • Related