Intersection of large number of arrays
4 views (last 30 days)
Show older comments
Hey there,
I want to solve the following problem. I have 2000 arrays of 500 strings each (500x1) and want to create an 3000x1 array of strings which appear most often in those 2000 initial arrays. I know intersection is not the right term for that, but I dont know how to explain it better. Any suggestions about the most efficient way to do that?
ps. The most obvious way to do that is to put all unique strings in an array followed by the number of times they appear, then sort the array and keep the first 3000 rows. However I am looking for a faster and more "sophisticated" way to do it.
thanks!
0 Comments
Answers (3)
Fangjun Jiang
on 3 Oct 2011
I think you'll have to do some kind of sort() or unique() operation.
My thought is: combine all strings in one big cell array, run
[B,I,J]=unique(BigCellArray)
Then, use function hist(J) to get the index of the most frequent occurrence.
Assume average 10 characters per string, 2000*500*10*2=20M bytes, no big deal!
0 Comments
Walter Roberson
on 3 Oct 2011
That approach is not bad, actually.
[ustrings, a, b] = unique(vertcat(A1,A2,A3,...,A2000));
counts = accumarray(b.', 1);
[scounts, sidx] = sort(counts, 'descending');
commonstrings = ustrings(sidx(1:min(end,3000)));
There are algorithms that would take less temporary memory, but the above will not copy the string contents themselves around, just references to the strings, so really it is fairly memory efficient and time efficient... and certainly a lot easier to code than the alternatives.
As to the effort to write out the vertcat() of the 2000 string arrays: if that proves to be a problem, then consider rewriting your program so that you Don't Do That.
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices 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!