How to display only once in a looping of 50 sample data

10 views (last 30 days)
Hi, I am looping a 50 sample size.
The output will be: (it display based on the number of underweight students out of 50 which is 8).
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Take more milk,protein shakes,rice,meat,nuts and butter
Is there anyway to make it display only one and together with the total number of students who are underweight?
Thank you in advance. Any opinions are welcome <3
I have attached my code below.
for i=1:50
if BMI(i)<18.5
BMI_status='Underweight';
if BMI_status=='Underweight'
fprintf('Take more milk,protein shakes,rice,meat,nuts and butter\n',BMI_status);
end
else
BMI_status='Normal';
if BMI_status=='Normal'
fprintf('You are healthy!\n',BMI_status);
end
end
end

Accepted Answer

Kishan Dhakan
Kishan Dhakan on 16 Jun 2021
You could store the data in a variable and use it later. Try this:
count = 0;
for i=1:50
if BMI(i)<18.5
BMI_status='Underweight';
if BMI_status=='Underweight'
count = count + 1;
end
else
BMI_status='Normal';
if BMI_status=='Normal'
fprintf('You are healthy!\n',BMI_status);
end
end
end
if count > 0
fprintf('Take more milk,protein shakes,rice,meat,nuts and butter\n',BMI_status);
fprintf('Total number of Underweight Students is %d',count);
end

More Answers (0)

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!