Remove rows of a table based on values in an array
3 views (last 30 days)
Show older comments
Emmanouil Barmpounakis
on 25 Sep 2019
Commented: Guillaume
on 25 Sep 2019
I want to remove all rows from a table, when a value in table.variable appears only once.
For example
table:
var1 | var2 | var3
1 | 2 | 0
1 | 2 | 3
1 | 2 | 3
1 | 2 | 8
4 | 5 | 8
1 | 2 | 9
should become
var1 | var2 | var3
1 | 2 | 3
1 | 2 | 3
1 | 2 | 8
4 | 5 | 8
0 Comments
Accepted Answer
KALYAN ACHARJYA
on 25 Sep 2019
Edited: KALYAN ACHARJYA
on 25 Sep 2019
disp('Original table');
var1=[1;1;1;1;4;1];
var2=[2;2;2;2;5;2];
var3=[0;3;3;8;8;9];
table_data=table(var1,var2,var3)
n=histc(var3(:),var3);
disp('Updated table');
table_data(find(n==1),:)=[]
Result Window:
Original table
table_data =
6×3 table
var1 var2 var3
__ ____ ____
1 2 0
1 2 3
1 2 3
1 2 8
4 5 8
1 2 9
Updated table
table_data =
4×3 table
var1 var2 var3
____ ____ ____
1 2 3
1 2 3
1 2 8
4 5 8
1 Comment
Guillaume
on 25 Sep 2019
table_data(n==1, :) = []
works just as well. The find is a waste of time.
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!