How to create a matrix from for loop result?

4 ビュー (過去 30 日間)
Hang Vu
Hang Vu 2019 年 4 月 28 日
コメント済み: Hang Vu 2019 年 4 月 29 日
I repelem the element by the index
D = [170, -80, -30, 0, -50, -30, 20, -60, 100, -60 -20];
Iplus=find(D>0);
for i=1:size(Iplus,2)
a=ceil(D(Iplus(i))/100);
A=repelem(Iplus(i),a*2)
end
How can I store all result as below into one matrix? like A=[1 1 1 1 7 7 9 9]
A =
1 1 1 1
A =
7 7
A =
9 9

採用された回答

Jos (10584)
Jos (10584) 2019 年 4 月 28 日
D = [170, -80, -30, 0, -50, -30, 20, -60, 100, -60 -20];
Iplus=find(D>0);
A = [] ; % initialize
for i=1:size(Iplus,2)
a=ceil(D(Iplus(i))/100);
Anew = repelem(Iplus(i),a*2)
A = [A Anew] % append
end
Note that Matlab will warn you, because A is growing every iteration. With some thinking you might be able to optimise or even vectorise this piece of code.
  1 件のコメント
Hang Vu
Hang Vu 2019 年 4 月 29 日
Thank you so much for the help!^^

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

その他の回答 (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