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

10 visualizaciones (últimos 30 días)
poor kid
poor kid el 15 de Jun. de 2021
Respondida: Kishan Dhakan el 16 de Jun. de 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

Respuesta aceptada

Kishan Dhakan
Kishan Dhakan el 16 de Jun. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by