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

10 vues (au cours des 30 derniers jours)
poor kid
poor kid le 15 Juin 2021
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

Réponse acceptée

Kishan Dhakan
Kishan Dhakan le 16 Juin 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by