'Iterate over columns in 2 separate numpy arrays
I currently have two 600 x 96 numpy arrays that I am trying to iterate over at the same time and apply a function to columns in the same index position in each array. When trying a nested for loop, the function returns 96^2 values instead of 96.
for i in range(SingleR.shape[1]):
for j in range(ConAvgFull.shape[1]):
lag_finder(SingleR[:,i], ConAvgFull[:,j], 500)
Here is what the function does.
from scipy import signal
import numpy as np
laglist = []
def lag_finder(y1, y2, sr):
y1 = y1.reshape(600, 1)
y2 = y2.reshape(600, 1)
n = len(y2)
corr = signal.correlate(y2, y1, mode='same') / np.sqrt(signal.correlate(y1, y1,
mode='same')[int(n/2)] * signal.correlate(y2, y2, mode='same')[int(n/2)])
delay_arr = np.linspace(-0.5*n/sr, 0.5*n/sr, n)
delay = delay_arr[np.argmax(corr)]
if np.argmax(corr) > 0:
laglist.append(delay)
How can I solve this?
Solution 1:[1]
Since you want to apply the function over the same column index, just use the same index:
for i in range(SingleR.shape[1]):
# We're using i in both arrays, accessing at the same index
lag_finder(SingleR[:,i], ConAvgFull[:,i], 500)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aaossa |
