# Categorize data in if else statement

14 views (last 30 days)
poor kid on 6 Jun 2021
Commented: poor kid on 8 Jun 2021
Hi, i am new to matlab and i have a question. Sorry if the questions is simple.
I have created a data (sample size=50)with height and weight to calculate BMI.
% Formula to calculate BMI
BMI=Weight./(Height).^2;
I have loop for my BMI based on the marks (eg: if BMI<18.5, disp('underweight')...) to categorize them into 'underweight','normal','overweight',etc.
% to loop my BMI and categorize it into 'underweight','normal'...
for i=1:50
if BMI(i)<18.5
disp('Underweight')
elseif BMI(i)>=18.5&&BMI(i)<24.9
disp('Normal weight')
elseif BMI(i)>=24.9&&BMI(i)<29.9
disp('Pre-obesity')
elseif BMI(i)>=29.9&&BMI(i)<34.9
disp('Obesity class I')
elseif BMI(i)>=34.9&&BMI(i)<39.9
disp('Obesity class II')
else
disp('Obesity class III')
end
end
After loop, it will display all the data in
underweight
obese
normal
underweight
.
.
.
for 50 sample size. Is there anyway to show only the total number of underweight , total number of normal , etc
Can I display the data in 'sum of underweight','sum of normal'... instead of displaying all the 50 datasets?
Any advice would be most appreciate.

Duncan Po on 7 Jun 2021
You can discretize the BMI values into categories and then count them, like this:
>> c = discretize(BMI, [0 18.5 24.9 29.9 34.9 39.9 50], "categorical", ["Under Weight", "Normal weight", "pre-obesity", "obesity I", "obesity II", "obesity III"]);
>> summary(c)
Under Weight 14
Normal weight 7
pre-obesity 1
obesity I 6
obesity II 8
obesity III 14
poor kid on 8 Jun 2021
Thank you !

### More Answers (2)

Manas Minnoor on 6 Jun 2021
Hello,
You may create an array of length 6, and increment a particular index of this array each time a particular weight category is accessed (you may put it after the display command). This way you are maintaining a counter for each weight category.
For a more elegant/complicated solution, you may have a look at Maps:
Hope this helps.
##### 1 CommentShowHide None
poor kid on 7 Jun 2021
Thanks !

Girijashankar Sahoo on 6 Jun 2021
Edited: Rik on 7 Jun 2021
Weight=randi([1 100],1,50);
Height=randi([1 3],1,50);
BMI=Weight./(Height).^2;
% to loop my BMI and categorize it into 'underweight','normal'...
for i=1:50
if BMI(i)<18.5
disp('Underweight')
str(i)=["Underweight"];
elseif BMI(i)>=18.5&&BMI(i)<24.9
disp('Normal weight')
str(i)=["Normal weight"];
elseif BMI(i)>=24.9&&BMI(i)<29.9
disp('Pre-obesity')
str(i)=["Pre-obesity"];
elseif BMI(i)>=29.9&&BMI(i)<34.9
disp('Obesity class I')
str(i)=["Obesity class I"];
elseif BMI(i)>=34.9&&BMI(i)<39.9
disp('Obesity class II')
str(i)=["Obesity class II"];
else
disp('Obesity class III')
str(i)=["Obesity class III"];
end
end
Underweight=sum(count(str,"Underweight"))
Normalweight=sum(count(str,"Normal weight"))
Pre_obesity=sum(count(str,"Pre-obesityt"))
Obesity_class_I=sum(count(str,"Obesity class I"))
Obesity_class_II=sum(count(str,"Obesity class II"))
Obesity_class_III=sum(count(str,"Obesity class III"))
##### 1 CommentShowHide None
poor kid on 7 Jun 2021
Hi, thank you for the answer. But it still showing the 50 variables for the loop part...
Results:
Underweight =
0
Normalweight =
0
Pre_obesity =
0
Obesity_class_I =
0
Obesity_class_II =
0
Obesity_class_III =
0
Normal weight
Underweight =
0
.
.
.
Btw, thank again !