Alternative to wcommon in 2011a
Show older comments
I need to find the indices of the values common to two vectors. I have one vector with 60 elements and another with about 80,000 and I want to see how many of the 60 are values within the 80K, and wheter they are in the array. They will not share indices. Ideally, the result will be an array 80K long with zeros for non-common elements and ones for the common elements. I have used wcommon before, but cannot do so on my current matlab installation. Time is of the essence. Your advice is greatly appreciated.
Answers (1)
Walter Roberson
on 5 Dec 2011
[tf, idx] = ismember(LongVector, ShortVector);
tf(K) will then be true if LongVector(K) appears in ShortVector, and idx(K) will be the index in ShortVector at which LongVector(K) appeared (and 0 if it did not appear.)
The number of common elements will be sum(tf)
Would you perhaps prefer to work the other way around,
[tf, idx] = ismember(ShortVector, LongVector);
The information content will be the same, but with a much shorter output array. This would be preferred unless you have a specific need to access LongVector with a logical index.
Categories
Find more on Resizing and Reshaping 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!