How is modeled the more short time this loop?

1 次查看(过去 30 天)
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 个评论
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