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
representative sample data:
from numpy import matrix, random, asmatrix, linalg, empty
B = random.random((156, 48))
A = B.shape
eig_val = empty(A, dtype=complex)
eig_vec = empty((A[0], A[1], A[1]), dtype=complex)
for i in range(int(A[0])):
data_process = B[i, :].reshape((A[1], 1))
rxx = data_process * matrix.getH(asmatrix(data_process)) / A[1]
eig_val[i:, ...], eig_vec[i:, ...] = linalg.eig(rxx)