Suppose I have a matrix N-by-K A, N-by-Pmatrix B. I want to do the following calculations to get the final matrix N-by-P X.
X(n,p) = B(n,p) - dot(gamma(p,:),A(n,:))
Where
gamma(p,k) = dot(A(:,k),B(:,p))/sum( A(:,k).^2 )
In MATLAB, I have my code like
for p = 1:P
for n = 1:N
for k = 1:K
gamma(p,k) = dot(A(:,k),B(:,p))/sum(A(:,k).^2);
end
x(n,p) = B(n,p) - dot(gamma(p,:),A(n,:));
end
end
which are very inefficient because they use three for loops! Is there a way to speed up this code?
source
share