How do I save outputs to a vector from if statement

MEI=[]
for node=0:dx:L;
if node>=0 && node<a
moment=@(x) (RA*x)
MEI=moment(node)
elseif node>=a && node<b
moment=@(x) (RA*x)-(Pa*(x-a))
MEI=moment(node)
elseif node>=b && node<=L
moment=@(x) (RA*x)-(Pa*(x-a))-(Pb*(x-b))
MEI=moment(node)
end
end
I want to save the outputs from the calculations to a 1 column vector. I can only save the last calculated value.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 27일
MEI(end+1) = moment(node)

댓글 수: 2

Greg
Greg 2016년 4월 27일
Thank you very much. Can you explain the logic behind that answer.
MEI(end) refers to the last element of MEI. MEI(end+1) refers to the location after the last element. Assigning to MEI(end+1) grows MEI to be large enough to fit the new value and then assigns the new value to the location.
Note that your conditions do not always assign a result. If they did always assign a result, it would be more efficient to assign a different approach.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 4월 27일

댓글:

2016년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by