Using an If Statement inside a While Loop

조회 수: 9 (최근 30일)
Jaden
Jaden 2014년 7월 7일
답변: Joseph Cheng 2014년 7월 7일
I have a code I am writing in which I need to incorporate an If statement inside of a While loop. I want my code to run equations a - d when a(2) > d and when a(2) <= d I want to pull that value out and put it into a separate vector. The issue I am having is that my loop stops after finding only one value of a(2) <= d, where I would like to find and pull out (place into separate vector) all values when a(2) <= d. Can anyone tell me what I am missing?
Pulsepileup.m :
a = rand(2,1)
b = round(a(1)*80+20)
c = find(energies100 == b)
d = spectrum100(c)
while a(2) > d
run pulsepileup.m
if a(2) <= d
x = b
end
end
Thank you!

답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 7월 7일
what you're missing is
x=[];
while a(2) > d
run pulsepileup.m
if a(2) <= d
x = [x b];
end
end
your original code stores the one value for when a(2)<=d. with the addition i made it'll concatenate to x when the condition is true.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by