how to store data for second looping

Hi everybody, dataA consist of 20 'y' values.
Then, if 'diff' is more than 0.01, the code will looping back. How i can store value of dataA for each 'z loop'? Thank you.
for z=1:1000;
for i=1:20
x(i)=rand
y(i)=2x(i)+1
dataA(i,:)=y
end
diff=max(y)-min(y)
if diff<0.01
return
else
z=z+1
end
end

 採用された回答

Aquatris
Aquatris 2020 年 6 月 1 日
編集済み: Aquatris 2020 年 6 月 2 日

0 投票

One way would be to use a 3D array for dataA variable or;
for z=1:1000
for i=1:20
x(i)=rand;
y(i)=2*x(i)+1;
end
dataA(z,:)=y;
diff=max(y)-min(y);
if diff<0.01
break;
end
end
You do not need to assign dataA to y(i) inside the loop.
You do not need to define z = z+1, "for loop" does it automatically.
Instead of return, I suggest you use break as well.

1 件のコメント

aleea razali
aleea razali 2020 年 6 月 2 日
hi, The coding works fine and the data can be save now. Thank you.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2020 年 6 月 1 日

編集済み:

2020 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by