How to create a matrix from for loop result?
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!