Categorize data in if else statement

2 ビュー (過去 30 日間)
poor kid
poor kid 2021 年 6 月 6 日
コメント済み: poor kid 2021 年 6 月 8 日
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
Duncan Po 2021 年 6 月 7 日
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
  3 件のコメント
Duncan Po
Duncan Po 2021 年 6 月 7 日
You can use a for loop instead of discretize to populate a categorical array:
c = categorical(nan(50,1),1:6,["Underweight", "Normal Weight", "Pre-obesity", "Obesity Class I", "Obesity Class II", "Obesity Class III"]); % preallocate with <undefined> elements
for i=1:50
if BMI(i)<18.5
c(i) = "Underweight";
elseif BMI(i)>=18.5&&BMI(i)<24.9
c(i) = "Normal weight";
elseif BMI(i)>=24.9&&BMI(i)<29.9
c(i) = "Pre-obesity";
elseif ...
end
poor kid
poor kid 2021 年 6 月 8 日
Thank you !

サインインしてコメントする。

その他の回答 (2 件)

Manas Minnoor
Manas Minnoor 2021 年 6 月 6 日
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 件のコメント
poor kid
poor kid 2021 年 6 月 7 日
Thanks !

サインインしてコメントする。


Girijashankar Sahoo
Girijashankar Sahoo 2021 年 6 月 6 日
編集済み: Rik 2021 年 6 月 7 日
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 件のコメント
poor kid
poor kid 2021 年 6 月 7 日
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 !

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by