필터 지우기
필터 지우기

How to create a matrix from for loop result?

조회 수: 1 (최근 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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by