storing loop function output in vector
이전 댓글 표시
Hi, I have a single column array of some positive and negative values. I am trying to use a loop to segregate them and to store the result in a separate vector. Unable to do ot so far. can anyone help please? This is my code:
Diff=MP_25-MP_60
y=nan(117,1)
for i=Diff(1:117) if i>0 y(i) = ('Upward') else y(i) = ('Downward') end end
채택된 답변
추가 답변 (1개)
Ali Ajaz
2018년 3월 18일
0 개 추천
댓글 수: 3
David Fletcher
2018년 3월 18일
편집: David Fletcher
2018년 3월 18일
for iter=1:length(Diff)
if (Diff(iter)>=0)
y(iter)='U'
else
y(iter)='D'
end
end
David Fletcher
2018년 3월 18일
You may find that because you pre-allocated y as NaN, when you add 'U' or 'D' you get their numeric codes instead of the letter.
you could pre-allocate y to a character vector instead
y=repmat('U',117,1)
David Fletcher
2018년 3월 18일
Actually, if you pre-allocate y to 'D' you can lose the else clause in the condition block (since the array is already set to 'D')
y=repmat('D',117,1)
for iter=1:length(Diff)
if (Diff(iter)>=0)
y(iter)='U'
end
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

