# Vectorization of Loops with Matrix Multiplications

2 vues (au cours des 30 derniers jours)
Tommaso Belluzzo le 30 Mar 2020
Hi all. I have the following code which is being repeated many times with different time series and this is producing a huge bottleneck on my code:
%% Example Data
t = 252;
h = randn(252,2);
l = round(0.1 * t,0);
%% Loop
for i = 1:(l - 1)
o_tmp = h(1,:).' * h(1+i,:);
for j = 2:(t - i)
o_tmp = o_tmp + h(j,:).' * h(j+i,:);
end
o_tmp = o_tmp / (t - i);
end
I would like ti know if there is a suitable way to improve it, maybe by vectorizing all computations or just some of them. Any suggestion is more than welcome. Thanks in advance for your help!
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
David Hill le 30 Mar 2020
It would help if you explained what you are trying to do (big picture).

Connectez-vous pour commenter.

### Réponse acceptée

Matt Shellhammer le 31 Mar 2020
%% Example Data
t = 252;
h = randn(252,2);
l = round(0.1 * t,0);
%% Loop
for i = 1:(l - 1)
o_tmp = (h(1:(t-i),:).' * h((1+i):t,:))/(t-i);
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

R2018a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by