Find elements in table without looping (Matlab)

As this seems to be a quite common problem, I have spent a while trying to find a suitable answer for my case in other posts. However, most of them are full of maths and I cannot see the solution very clear.
This is my case:
I generated a function that imports data into Matlab in form of a table. The table looks like:
This table has around one million of rows. Basically, I need to extract from this table just the rows where the table1.ID matches 'DHK' and leave the result in another table.
This is the code I have just now:
cont=0;
for p=1:length(table1.ID)
ID=table1.ID(p,1);
IDstr=ID{1,1};
if strcmp(IDstr,'DHK')==1
cont=cont+1;
var1(cont,1)=table1.var1(p,1);
var2(cont,1)=table1.var2(p,1);
var3(cont,1)=table1.var3(p,1);
var4(cont,1)=table1.var4(p,1);
end
end
table2=table(var1,var2,var3,var4);
This code works but it takes extremely long time as I am dealing with a million of rows and many import files. There migth be a better way or command that automatically finds the information that I need instead of looking for it line by line?. I found this MATLAB link on the documentation but I am not sure how to apply it for what I need: http://uk.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html
I would be very grateful if somebody could help me.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 5 日
編集済み: Azzi Abdelmalek 2015 年 6 月 5 日

3 投票

v={'ID' 'var1' 'var2' ;'DHK' 12 14; 'ABC',15 20;'DHK',15 13}
w=cell2table(v(2:end,:),'variablenames',v(1,:)) % Example
out=w(ismember(w.ID,'DHK'),:)

3 件のコメント

Edward Moran
Edward Moran 2019 年 7 月 14 日
I want to do the same thing as asked in this question except that I want to look for more than just 'DHK'. I want to search for 'DHK' and, say, 'ABC' at the same time without writing a loop.
I want to avoid having to write the following:
out=w(ismember(w.ID,'DHK'),:)
out2=w(ismember(w.ID,'ABC'),:)
I have several thousand of these variable like 'DHK' and 'ABC' in a table. I want to search without writing a loop to create one table with all the occurrences of these variables. I need it in just a few lines if possible. I need the results of 'DHK' and 'ABC' in the same table.
Thanks!
Edward Moran
Edward Moran 2019 年 7 月 14 日
% this is the solution I've come up with and it works! Thank you, Edward!
myKeys = string({'DHK';'ABC'});
idx = ismember(T.Var1, myKeys);
% Access the table for these specific rows
out=T(idx,:)
Francesco Giuseppe Fornari
Francesco Giuseppe Fornari 2019 年 11 月 11 日
hi,
what if I wanted to find 'DHK' without knowing in which columns it can be?
I've got multiple csv files, where I need to find the same string.
thanks

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

その他の回答 (2 件)

Gabor
Gabor 2019 年 3 月 9 日

3 投票

rows = T.id=='DHK';
T(rows, :)
Peter Perkins
Peter Perkins 2015 年 6 月 5 日

1 投票

First convert those highly repetitive strings to a categorical variable:
table1.Var1 = categorical(table1.Var1);
Type whos before and after doing that and watch your memory use decrease. Then do more or less what Azzi suggests, leveraging the categorical equality operator:
table2 = table1(table1.ID == 'DHK',:);
That's it!

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by