필터 지우기
필터 지우기

Categorize data in if else statement

조회 수: 4 (최근 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.

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by