Python Forum

Full Version: 3D covariance matrix - vectrorizing python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to speed up a python code, I would like to avoid the use of the following for cycle, where "data" matrix has dimension [dim1xdim2]:

for i in range(int(dim1)):
        data_process = data[i,:].reshape((dim2, 1))
        rxx = data_process * np.matrix.getH(np.asmatrix(data_process)) / dim2
Using the 'for cycle' the dimension of the rxx matrix is [dim2xdim2], I would get a 3D "rxx" matrix [dim1xdim2xdim2]. I tried to use the following solution:

data_new = repeat(data_process0[:, :, newaxis], dim2, axis=2)
 N_2 = data_new.shape[2]
 m1 = data_new - data_new.sum(2, keepdims=1) / N_2
 y_out = einsum('ijk,ilk->ijl', m1, m1) / (N_2 - 1)
In this case I got 3D "y_out" matrix [dim1xdim2xdim2] but this doesn't work in my case.

Thanks,
Luca