roos_B
roos_B

Reputation: 11

Cumulating values in a 2D array

I have created a 2d array ( Ncreated ) which shows how many stars are formed in certain time steps ( dt ) I have then used the np.where to state that star formation stops at 1 billion years. So, the array only goes up to row 4 in time. This is what I want. But now, I want to cumulate the values, only in the columns, so that after row 4 there should be a separate constant value in each column. This is Nsum. However, I can't get this to work as I feel the np.where is effecting the value. How would I go about correcting defining Nsum to create the array I need? Thanks for any help.

M = arange(0.5,25.5,0.5)
xi0 = 1
scalefactor = 14.35
Nyear = scalefactor * xi0 * M**-2.35


N = 50

dt = np.zeros(N)
t = logspace(0, log10(tgal), N)

for i in range(1,N):
    dt[i] = t[i] - t[i-1]
dt[0] = 0.1


Ncreated = np.zeros((len(t),len(M)))
Nsum = np.zeros((len(t),len(M)))

for i in range(0,len(t)):
    for j in range(0,len(M)):
        Ncreated[i,j] = (Nyear[j] * dt[i]) 


d, = np.where(t > log10(1e9))
Ncreated[d] = 0

for i in range(0,len(t)):
    for j in range(0,len(M)):
        Nsum[i,j] = Ncreated[i,j] - Ncreated[[i-1],[j-1]]

Upvotes: 0

Views: 25

Answers (1)

roos_B
roos_B

Reputation: 11

Can be solved with

Nsum = cumsum(Ncreated, axis = 0)

Upvotes: 1

Related Questions