フィルターのクリア

How to fix this loop to get the desired output ?

1 回表示 (過去 30 日間)
Aswin Sandirakumaran
Aswin Sandirakumaran 2018 年 5 月 23 日
コメント済み: Majid Farzaneh 2018 年 5 月 23 日
New_Td = [4,1,1]
for t = New_Td
for b = 1:t
duplicate(b) = 0;
r(b) = 0;
fprintf('Hai from %i\n',t);
disp(duplicate);
end
end
Output I am getting look like this: ->>>> WRONG OUTPUT WHICH I AM GETTING
if true
Hai from 4
0
Hai from 4
0 0
Hai from 4
0 0 0
Hai from 4
0 0 0 0
Hai from 1
0 0 0 0
Hai from 1
0 0 0 0
end
But CORRECT OUTPUT SHOULD BE LIKE THIS:
if true
Hai from 4
0
Hai from 4
0 0
Hai from 4
0 0 0
Hai from 4
0 0 0 0
Hai from 1
0
Hai from 1
0
end

採用された回答

Majid Farzaneh
Majid Farzaneh 2018 年 5 月 23 日
Hi,
New_Td = [4,1,1]
for t = New_Td
duplicate=[];
for b = 1:t
duplicate(b) = 0;
r(b) = 0;
fprintf('Hai from %i\n',t);
disp(duplicate);
end
end
  1 件のコメント
Majid Farzaneh
Majid Farzaneh 2018 年 5 月 23 日
At the first time for t=4, duplicate length become 4. When t become 1, duplicate length is still 4.
You should destroy duplicate and build it again after each iteration. The code above is work.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by