MATLAB Answers

Creating new table from another table

3 views (last 30 days)
I have a 4 column table with ages, FEV values, Heights and the last has 1s or 0s. 1 means they smoke, 0 means they don't.
I need to make a new table with ages 11 and up, who smoke, and include their FEV values

Accepted Answer

Star Strider
Star Strider on 18 Sep 2021
There are several ways to do this, including using the findgroups function.
A relatively efficient way is just to do the comparisons to create a logical vector (‘Lv’ here), and go with that —
T0 = table(randi([5 90],20,1), 100*rand(20,1),randi([100 200],20,1),randi([0 1],20,1), 'VariableNames',{'Age','FEV_1','Height','Smokes'})
T0 = 20×4 table
Age FEV_1 Height Smokes ___ _______ ______ ______ 77 55.903 160 1 11 0.77497 112 1 77 10.859 132 1 65 82.203 173 1 48 58.998 103 0 62 92.198 162 1 87 22.737 183 0 10 39.319 176 0 31 23.808 113 1 86 29.428 145 0 67 54.328 116 1 78 31.662 121 0 5 33.127 123 1 82 77.932 138 0 5 32.548 145 0 22 35.402 200 1
Lv = T0{:,1}>=11 & T0{:,4}==1;
T1 = T0(Lv,:)
T1 = 11×4 table
Age FEV_1 Height Smokes ___ _______ ______ ______ 77 55.903 160 1 11 0.77497 112 1 77 10.859 132 1 65 82.203 173 1 62 92.198 162 1 31 23.808 113 1 67 54.328 116 1 22 35.402 200 1 34 19.997 133 1 12 78.177 113 1 19 66.497 185 1
Experiment to get different results.
.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!

Translated by