- What units are WEIGHT in? Look at the values and decide if they are reasonable for those units.
- What units are HEIGHT in? Look at the values and decide if they are reasonable for those units.
- What value of BMI do you expect will ever make this TRUE?: (28.00 < patient.BMI(i)) && (patient.BMI(i) < 3). Please tell me one number which is both less than 3 and greater than 28.
- Note that MATLAB does not have a n-ary logical comparison. The logical comparison operators are all bivariate functions. Repeated comparisons are evaluated from left to right. So your code:
Create new column in a table to be filled based on the other columns
27 次查看(过去 30 天)
显示 更早的评论
Hi! Is there a way i can create a new column called 'Check' to get populated with a number depending on other columns? And from there create another column called 'Test' which determines which patient to test based on the new column 'Check'?
Age = [38;43;38;40;49;56;82;10;15;34;38;12];
Weight = [71;69;64;67;64;60;70;55;80;50;76;98];
Height = [176;163;131;133;119;175;165;148;163;156;174;169]
patient = table(Age, Weight, Height)
patient.BMI = (patient.Weight*0.453592)./(patient.Height*0.0254).^2
%how do i create a new column called 'Check' that gets assigned the numbers 0 to 4, depending on these criteria?
for i = 1:length(patient.BMI)
if patient.BMI(i) > 2.6
Check(i) = 1;
elseif 2.4 < patient.BMI(i) < 2.5 && patient.Height(i) > 150 && patient.Weight(i) > 60
Check(i) = 2;
elseif 2.4 < patient.BMI(i) > 3 && patient.Weight(i) > 80
Check(i) = 3;
elseif (2.4 < patient.BMI(i)) && (patient.BMI(i) < 3) && (patient.Height(i) < 160) && (patient.Weight(i) < 90)
Check(i) = 4;
else
Check(i) = 0;
end
end
%is there a way to add another column called "Test" where if 'Check' is 0, it gets filled with 'No', if 'Check' is 1, then it gets filled with 'No', if 'Check' is 3 then it gets filled with 'Yes' and if 'Check' is 4, it gets filled with 'Maybe'
1 个评论
Stephen23
2024-2-6
编辑:Stephen23
2024-2-6
Before moving on to that column, you should check the rest of your code:
28.00 < patient.BMI(i) < 2.5
is thus equivalent to this:
(28.00 < patient.BMI(i)) < 2.5
which is therefore equivalent to either of these:
true < 2.5
false < 2.5
and thus is trivially (because true=1 and false=0) always
true
In every case where a you write A<B<C you probably want A<B && B<C (and similarly for all other logical comparisons).
Even if MATLAB had chainable logical comparisons, what value is both less than 2.5 and greater than 28?
So you have a few things to fix...then you can move on to generating that text column.
回答(1 个)
Avni Agrawal
2024-2-6
Hi Lavenia,
I understand that you want to fill details into new column based on other columns. You can achieve this by using logical indexing and the `table` function in MATLAB. Below is the code to create the 'Check' column based on the specified criteria and then create the 'Test' column accordingly:
Age = [38;43;38;40;49;56;82;10;15;34;38;12];
Weight = [71;69;64;67;64;60;70;55;80;50;76;98];
Height = [176;163;131;133;119;175;165;148;163;156;174;169];
patient = table(Age, Weight, Height);
% Calculate BMI
patient.BMI = (patient.Weight*0.453592)./(patient.Height*0.0254).^2;
% Create 'Check' column based on criteria
Check = zeros(size(patient,1), 1); % Initialize 'Check' column
for i = 1:size(patient,1)
if patient.BMI(i) > 2.6
Check(i) = 1;
elseif 28.00 < patient.BMI(i) < 2.5 && patient.Height(i) > 150 && patient.Weight(i) > 60
Check(i) = 2;
elseif 28.00 < patient.BMI(i) > 3 && patient.Weight(i) > 80
Check(i) = 3;
elseif (28.00 < patient.BMI(i)) && (patient.BMI(i) < 3) && (patient.Height(i) < 160) && (patient.Weight(i) < 90)
Check(i) = 4;
else
Check(i) = 0;
end
end
% Create 'Test' column based on 'Check' column
Test = cell(size(patient,1), 1); % Initialize 'Test' column
Test(Check == 0 | Check == 1) = {'No'};
Test(Check == 2 | Check == 3) = {'Yes'};
Test(Check == 4) = {'Maybe'};
% Add 'Check' and 'Test' columns to the patient table
patient.Check = Check;
patient.Test = Test;
disp(patient);
This code first calculates the BMI and then iterates over each row to determine the value of the 'Check' column based on the specified criteria. Afterwards, it assigns 'No', 'Yes', or 'Maybe' to the 'Test' column based on the value of the 'Check' column. Finally, both 'Check' and 'Test' columns are added to the 'patient' table.
I hope this helps.
3 个评论
Stephen23
2024-2-6
"Undefined function 'gscatter' for input arguments of type 'double'."
GSCATTER is in the Statistics and Machine Learning Toolbox:
To use GSCATTER you would need to have that toolbox installed and a valid license for it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Manage Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!