Using an If Statement inside a While Loop

13 views (last 30 days)
Jaden
Jaden on 7 Jul 2014
Answered: Joseph Cheng on 7 Jul 2014
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!

Answers (1)

Joseph Cheng
Joseph Cheng on 7 Jul 2014
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!