How is modeled the more short time this loop?

조회 수: 1 (최근 30일)
Erkan
Erkan 2021년 8월 28일
댓글: Erkan 2021년 8월 28일
Hi everybody, i have the for loop, but loop time is very long. How can i make the more short time?
R=rand(1,3000);
W=zeros(1,length(R));
for i=1:length(R)+1
for ii=1:length(R)-i+1
W(i)=W(i)+R(ii)*R(ii+i-1)
end
end

채택된 답변

DGM
DGM 2021년 8월 28일
편집: DGM 2021년 8월 28일
I bet this can be simplified further, but here.
l = 4;
R = rand(1,l);
W = zeros(1,l);
for k = 1:l
W(k) = sum(R(1:l-k+1) .* R(k:end));
end
EDIT: Updated to match question change. Also, echoing to console takes a considerable amount of time.
  댓글 수: 4
DGM
DGM 2021년 8월 28일
Edited.
Erkan
Erkan 2021년 8월 28일
Yes, it is right working, thank you so much.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by