Categorize data in if else statement
조회 수: 14 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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
추가 답변 (2개)
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
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"))
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!