Table rows filtering using vector
이전 댓글 표시
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
Eric Sofen
2019년 4월 2일
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
...
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!