getting new tables from another

조회 수: 1(최근 30일)
jean claude
jean claude 2018년 10월 26일
댓글: Peter Perkins 2018년 10월 31일
hi, how to get table just for smith; a table for william , and a table for johnson ? imagine i have many rows with different company names and i want to separate them, so getting a table for every company. Here is a simplified example
LastName = {'Smith';'Johnson';'William';'William';'Smith'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...
LastName)
output wanted x =
Age Height Weight BloodPressure LastName
___ ______ ______ _____________ ________
38 71 176 124 93 'Smith'
49 64 119 122 80 'Smith'
for william
Age Height Weight BloodPressure LastName
___ ______ ______ _____________ _________
38 64 131 125 83 'William'
40 67 133 117 75 'William'
  댓글 수: 2
Steven Lord
Steven Lord 2018년 10월 26일
I second Stephen's comment. If you had a table with 1000 unique company names, do you really want to create 1000 individual variables in the workspace? That's highly discouraged.
In addition to splitapply and findgroups which Stephen mentioned, there are functions like groupsummary (introduced in release R2018a) and grouptransform (introduced in release R2018b) that may make your workflow with the one larger table easier.
Perhaps if you tell us more about how you're planning to use those small company-specific table arrays we can offer suggestions for how to achieve your goal without creating lots of individual variables.

댓글을 달려면 로그인하십시오.

답변(1개)

madhan ravi
madhan ravi 2018년 10월 26일
편집: madhan ravi 2018년 10월 26일
LastName = {'Smith';'Johnson';'William';'William1';'Smith1'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...,
'RowNames', LastName)
smith = T('Smith',:) %creates new table for smith
johnson = T('Johnson',:) %creates new table for Johnson
william = T('William',:) %creates new table for William
COMMAND WINDOW DISPLAYS:
T =
5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Johnson 43 69 163 109 77
William 38 64 131 125 83
William1 40 67 133 117 75
Smith1 49 64 119 122 80
smith =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
johnson =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Johnson 43 69 163 109 77
william =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
William 38 64 131 125 83
>>
  댓글 수: 6
Peter Perkins
Peter Perkins 2018년 10월 31일
Do a strcmp on the table variable containing the names, and use the logical vector from that as a row subscript on the original table.
But you should considered heeding Stephen and Steve's advice

댓글을 달려면 로그인하십시오.

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by