Table delete rows with specific value in column

I will ask my question by an example. I have a table, looks like:
T = table();
T.c1 = [1 2 1 1 1 2 1 2 2 ]';
T.c2 = ['a' 'a' 'a' 'b' 'b' 'a' 'a' 'a' 'b']';
T.c3 = ['a' 'b' 'a' 'a' 'b' 'a' 'b' 'a' 'a']';
T.c4 = [1 1 1 2 1 2 1 3 2 ]';
I want to delete all the rows (The complete row) that have 'b' in c2. Also, I want to delete all rows where c4 > c1. How can I do this efficiently?

 採用された回答

Rik
Rik 2018 年 2 月 2 日
編集済み: Rik 2018 年 2 月 2 日

3 投票

T = table();
T.c1 = [1 2 1 1 1 2 1 2 2 ]';
T.c2 = ['a' 'a' 'a' 'b' 'b' 'a' 'a' 'a' 'b']';
T.c3 = ['a' 'b' 'a' 'a' 'b' 'a' 'b' 'a' 'a']';
T.c4 = [1 1 1 2 1 2 1 3 2 ]';
T(ismember(T.c2,'b'),:)=[];
T(T.c4>T.c1,:)=[];

4 件のコメント

Aletta Wilbrink
Aletta Wilbrink 2018 年 2 月 2 日
Thank you for your help!
Rik
Rik 2020 年 2 月 5 日
Comment posted as answer by Matteo Soldini:
If I want to keep only the rows that have a certain value in a column (for example, all the rows that have 'b' in c2) and delete all the others, what should I do?
Rik
Rik 2020 年 2 月 5 日
This code uses logical indexing, so you can just invert it. ismember returns a logical array, nothing is stopping you from doing something like this:
T(~ismember(T.c2,'b'),:)=[];
Matteo Soldini
Matteo Soldini 2020 年 2 月 5 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by