MATLAB Answers

How to vectorize this code? element wise to save time

1 view (last 30 days)
MIDO
MIDO on 19 Apr 2020
Answered: Vimal Rathod on 26 Apr 2020
% size(matAL) > 32,500,5
matCT=zeros(500,32,32); %Pre-allocate
x=0;
for t=1:500
x=1+x;
for i=1:32
for j=1:32
xsd = squeeze(matAL(i,t,:) .* conj(matAL(j,t,:)));
matCT(x,j,i) = abs(mean(exp(1i*angle(xsd))));
end
end
end

  0 Comments

Sign in to comment.

Answers (1)

Vimal Rathod
Vimal Rathod on 26 Apr 2020
Hi,
As all the three for loops have some dependencies it is difficult to vectorize the code.
you can try the following code, it is partially vectorized.
for i = 1:32
for j = 1:32
xsd = squeeze(matAL(i,1:500,:) .* conj(matAL(j,1:500,:)));
matCT(1:500,j,i) = abs(mean(exp(1i*angle(xsd(1:500,:)))'));
end
end

  0 Comments

Sign in to comment.

Products


Release

R2018b