Creating new table from another table

조회 수: 13 (최근 30일)
Colin Leonelli
Colin Leonelli 2021년 9월 18일
답변: Star Strider 2021년 9월 18일
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

채택된 답변

Star Strider
Star Strider 2021년 9월 18일
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.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by