Vectorization of Loops with Matrix Multiplications

조회 수: 1 (최근 30일)
Tommaso Belluzzo
Tommaso Belluzzo 2020년 3월 30일
답변: Matt Shellhammer 2020년 3월 31일
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
David Hill
David Hill 2020년 3월 30일
It would help if you explained what you are trying to do (big picture).

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Shellhammer
Matt Shellhammer 2020년 3월 31일
%% 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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by