else if statements on a table
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a 60x12 table. Each row has patient data and the overall aim is using if and else if statements determine if the patients suffers from a particular disease. I am struggling to understand how to get the code of if and else if statements to analysis each row individually and produce an outcome for each row using my if and else if statements
0 Kommentare
Antworten (1)
KL
am 16 Nov. 2017
You do not need if-else to filter data when you use a table. It would have been easier had you attached some sample data, aynway let's take the example from documentation.
First the table,
load patients
patients = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName);
T = patients(1:10,:);
Now, create a dummy data as a disease column,
Disease = logical(round(rand(10,1))); %dummy data
T.Disease = Disease; %add the column to the table
My table with this new column looks like,
T =
10×6 table
Age Gender Height Weight Smoker Disease
___ ________ ______ ______ ______ _______
Smith 38 'Male' 71 176 true false
Johnson 43 'Male' 69 163 false false
Williams 38 'Female' 64 131 false true
Jones 40 'Female' 67 133 false true
Brown 49 'Female' 64 119 false true
Davis 46 'Female' 68 142 false false
Miller 33 'Female' 64 142 true true
Wilson 40 'Male' 68 180 false false
Moore 28 'Male' 68 183 false false
Taylor 31 'Female' 66 132 false false
Now, if you want to see all the patients' details who have this disease,you say,
T(T.Disease==1,:)
result would be something like,
ans =
4×6 table
Age Gender Height Weight Smoker Disease
___ ________ ______ ______ ______ _______
Williams 38 'Female' 64 131 false true
Jones 40 'Female' 67 133 false true
Brown 49 'Female' 64 119 false true
Miller 33 'Female' 64 142 true true
if you want to check if a certain patient has this disease you say,
T('Smith','Disease')
then you get,
ans =
table
Disease
_______
Smith false
you can also combine multiple conditions like,
T(T.Smoker==1&T.Disease==1,:)
you get the result as,
ans =
1×6 table
Age Gender Height Weight Smoker Disease
___ ________ ______ ______ ______ _______
Miller 33 'Female' 64 142 true true
Hope you get an idea of how it works.
1 Kommentar
AluminiumMan
am 19 Sep. 2022
This is really useful. What about if the data is equal to or greater than a certain value? Rather than equalling a certain value?
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!