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

12 次查看(过去 30 天)
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

采纳的回答

Kishan Dhakan
Kishan Dhakan 2021-6-16
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

更多回答(0 个)

类别

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