Alternative to wcommon in 2011a

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)

[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.

1 Comment

This was exactly right. Also, you were right about the shorter vector - this was actually what I had been doing already, but in my confusion/rush I forgot and thought that I needed the longer one. Thanks for the help!

Sign in to comment.

Categories

Asked:

on 5 Dec 2011

Community Treasure Hunt

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

Start Hunting!