Using an If Statement inside a While Loop

13 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by