MATLAB - extract selected rows in a table based on some criterion

这一生的挚爱 提交于 2019-12-07 18:37:15

问题


Let's say I have a table like this:

post   user         date
____   ____   ________________
 1      A     12.01.2014 13:05
 2      B     15.01.2014 20:17
 3      A     16.01.2014 05:22

I want to create a smaller table (but not delete the original one!) containing all posts of - for example - user A including the dates that those were posted on.

When looking at MATLAB's documentation (see the very last part for deleting rows) I discovered that MATLAB allows you to create a mask for a table based on some criterion. So in my case if I do something like this:

postsA = myTable.user == 'A'

I get a nice mask vector as follows:

>> postsA = 
       1
       0
       1

where the 1s are obviously those rows in myTable, which satisfy the rule I have given.

In the documention I have pointed at above rows are deleted from the original table:

postsNotA = myTable.user ~= 'A' % note that I have to reverse the criterion since I'm choosing stuff that will be removed
myTable(postsNotA,:) = [];

I would however - as stated above - like to not touch my original table. One possible solution here is to create an empty table with two columns:

post  date
____  ____

interate through all rows of my original table, while also looking at the current value of my mask vector postsA and if it's equal to 1, copy the two of the columns in that row that I'm interested in and concatenate this shrunk row to my smaller table. What I'd like to know is if there is a more or less 1-2 lines long solution for this problem?


回答1:


Assuming myTable is your original table.

You can just do

myTable(myTable.user == 'A',:)

Sample Code:

user = ['A';'B';'A';'C';'B'];
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(user,Age,Height,Weight,BloodPressure)
T(T.user=='A',:)

Gives:

T =

user    Age    Height    Weight          BloodPressure      
____    ___    ______    ______    _________________________

A       38     71        176       124                    93
B       43     69        163       109                    77
A       38     64        131       125                    83
C       40     67        133       117                    75
B       49     64        119       122                    80

ans =

user    Age    Height    Weight          BloodPressure      
____    ___    ______    ______    _________________________

A       38     71        176       124                    93
A       38     64        131       125                    83


来源:https://stackoverflow.com/questions/27300187/matlab-extract-selected-rows-in-a-table-based-on-some-criterion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!