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

11 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Kishan Dhakan
Kishan Dhakan il 16 Giu 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by