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 日

0 投票

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 日
Thank you so much, i will examine and write the answer
Erkan
Erkan 2021 年 8 月 28 日
Hi DGM, firstly i have mistaked while writing the question;
it must be W(i)=W(i)+R(ii)*R(ii+i-1). For this state i excuse me. if it possible, can you examine again?
DGM
DGM 2021 年 8 月 28 日
Edited.
Erkan
Erkan 2021 年 8 月 28 日
Yes, it is right working, thank you so much.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

質問済み:

2021 年 8 月 28 日

コメント済み:

2021 年 8 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by