Categorize data in if else statement
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
poor kid
el 6 de Jun. de 2021
Comentada: poor kid
el 8 de Jun. de 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.
0 comentarios
Respuesta aceptada
Duncan Po
el 7 de Jun. de 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
3 comentarios
Duncan Po
el 7 de Jun. de 2021
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
Más respuestas (2)
Manas Minnoor
el 6 de Jun. de 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.
Girijashankar Sahoo
el 6 de Jun. de 2021
Editada: Rik
el 7 de Jun. de 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"))
Ver también
Categorías
Más información sobre Numeric Types 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!