ShinyPebble
ShinyPebble

Reputation: 135

Python: Collapsing arrays of arrays into each other without for loops

Suppose I have multiple NxN 2D arrays stored into a list in Python 3. I want to collapse all the arrays into 1 array, with the same dimensions NxN, but such that each element of this new array contains a 1xN array of the corresponding values from the original arrays.

To give you some more context, each array in this list corresponds to the set of values at a given time. For each new time point, I am storing the updated version of that array into the list. Once that's done, I want to compute the standard deviation of the values at each (i,j) element in the array.

I tried using a for loop, but it takes far too long for my simulations because this is a set of 100,000 arrays. I was wondering if there were any numpy or vectorized functions that can help me perform this operation more efficiently. Thanks!

Upvotes: 0

Views: 521

Answers (1)

Ehsan
Ehsan

Reputation: 12407

Lets say l is your list of arrays. You need to get std of corresponding elements of those arrays into a single array:

std_l = np.std(np.stack(l),axis=0)

Upvotes: 2

Related Questions