Table rows filtering using vector
Show older comments
I have a table (11207552 x 9) from which I want to select the data for the entire row if the value in colum 1 matches any value in vector idn (177 values) for each idn there are around 3000 consecutive same values in my table.

Both the table and vector are in ascending order.
Colum 3 and 8 are strings, colum 2 is datetime and rest are double
I have tried the below code it runs fine but the values in the new table (a) are all 0, <missing> and NaT.
How do I get the values from table b into table a ?
for j=1:177
test=idn(j);
for i=1:11207552
if table2array(b(i,1))==test
a(i,:)= b(i,:);
end
end
end
1 Comment
Eric Sofen
on 2 Apr 2019
You can do this with a single indexing expression, where you logically index into id. Here's a smaller example to illustrate. The idea spans values 1:5, let's select out all the rows with an id of 1 or 3:
>> t = table(randi(5,[100,1]), randn([100,1]),'VariableNames', {'id','data'});
head(t)
ans =
8×2 table
id data
__ __________________
1 -0.614487094685593
1 -0.255684360740088
5 -1.12902046539328
5 2.23627817005166
1 -1.86697703038261
2 0.465878361815095
1 -0.382380596738338
2 0.449432965777516
>> t1 = t(any(t.id == [1,3],2),:)
t1 =
40×2 table
id data
__ ___________________
1 -0.614487094685593
1 -0.255684360740088
1 -1.86697703038261
1 -0.382380596738338
3 0.30338509990999
1 0.633698919484325
3 -0.61973110598596
...
Answers (0)
Categories
Find more on Numeric Types 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!