The best way to input a table with a unique format

3 views (last 30 days)
Hi all,
I have a table that has the following format-
tabletobuild.png
I am trying to find the best method to build the table in matlab.
I have tried the following -
systolic=[-inf 119;-inf 119; 120 129;120 129]
diastolic=[-inf 79;-inf 79; 80 84;80 84]
gender={'Female'; 'Male'; 'Female'; 'Male' }
value= [-3; 0; 0;0]
bpt= table(systolic, diastolic, gender, value)
but I only get the diagonal values.
Any help would be very much appreciated.
Best,
Andrew
  5 Comments
Adam Danz
Adam Danz on 13 Mar 2019
The question isn't clear to me. Are you asking wether the format of this table will work with your project?
Andrew Czeizler
Andrew Czeizler on 13 Mar 2019
Hi Adam!
I'm asking the best method to input the table into matlab, so to be able to perform operations on the table.
many thanks
best,
Andrew

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 13 Mar 2019
Edited: Adam Danz on 14 Mar 2019
(Continuing from comment section under the question)
The best format for a table depends on how you plan on interacting with the table. Basic tidy data principles suggest forming tables that have one row per observation and one column per variable. That makes it really easy to access and interact with single variables. Your current table is organized such the systolic and diastolic columns contain [1 x 2] vectors of [low, high] ranges. You could split that into their own columns like this.
systolic=[-inf 119;-inf 119; 120 129;120 129]
diastolic=[-inf 79;-inf 79; 80 84;80 84]
systolicLow = systolic(:,1);
systolicHigh = systolic(:,2);
diastolicLow = diastolic(:,1);
diastolicHigh = diastolic(:,2);
gender={'Female'; 'Male'; 'Female'; 'Male' }
value= [-3; 0; 0;0]
bpt= table(systolicLow, systolicHigh, diastolicLow, diastolicHigh, gender, value)
bpt =
4×6 table
systolicLow systolicHigh diastolicLow diastolicHigh gender value
___________ ____________ ____________ _____________ ________ _____
-Inf 119 -Inf 79 'Female' -3
-Inf 119 -Inf 79 'Male' 0
120 129 80 84 'Female' 0
120 129 80 84 'Male' 0
Now it will be easier to interact with the data. For example, which row classifies the following data?
syst = 122;
dias = 81;
gender = 'Female';
rowIdx = bpt.systolicLow < syst & bpt.systolicHigh >= syst & ...
bpt.diastolicLow < dias & bpt.diastolicHigh >= dias & ...
strcmpi(bpt.gender, gender);
bpt(rowIdx,:)
ans =
1×6 table
systolicLow systolicHigh diastolicLow diastolicHigh gender value
___________ ____________ ____________ _____________ ________ _____
120 129 80 84 'Female' 0
bpt.value(rowIdx,:)
ans =
0

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!