counting strings in cell array, is there a faster solution

1 回表示 (過去 30 日間)
Scragmore
Scragmore 2012 年 1 月 27 日
コメント済み: Manduna Watson 2014 年 6 月 30 日
Hi all.
I have two cell array's test1 240000x1 and test2 160000x1. Each cell of test1 contains a string, varying lengths 1-20 charicters. test2 is a list of unique entries from test1.
I wish to count the number of occurrences of each unique string in test2 in test1.
example strings in test1 & 2
test1 = {'ayooy'; 'ayta'; 'a'; 'aa'; 'aatl'; 'aatla'; ......};
test2 = {'a'; 'aa'; 'aaa'; 'aaaa'; 'aaaaa'; 'aaaac'; .......};
My code;
for ii = 1:length(test2)
b = ismember(test1,test2(ii,1));
test2{ii,2}(1,1) = sum(b);
end
Is there a way to speed this up or an alternative method that is faster. I know I am running a lookup that is 160k * 240k = 40,000 mill.
Thanks for you time
AD

採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 27 日
When you construct test2, use a different form of unique:
[test2, ua, ub] = unique(test1);
After that, the counts are:
test2counts = histc(ub, 1:length(test2));
  2 件のコメント
Scragmore
Scragmore 2012 年 1 月 28 日
Thanks for highlighting the additional output of unique and introducing me to new function histc. Worked a treat, supper fast compared to what I was originally doing.
Cheers,
AD
Manduna Watson
Manduna Watson 2014 年 6 月 30 日
Thank you, this really helped me to identify and count repeated characters in my data set

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by