Finding intersecting rows in column vectors of varying length?

Hello, I have 2 column vectors of different lengths and I would like to find instances in each vector with the same value. For example if vector 1 is:
v1x=[1:0.1:10];
v1y=[20:2:200];
And vector 2 is:
v2x=[1;6.5;9;10;10;1;2.3];
v2y=[20;62;26;30;200;96;200];
I would like to create a 3rd column vector with each “intersecting” instance such that:
v1x==v2x and v1y==v2y.
This should return for the example above, row =1 ,where
v1x=1=v2x and v1y=20=v2y
and
row =5, where v1x=10=v2x and v1y=200=v2y.
Thanks
Gareth

 Accepted Answer

>> [~,~,ix]=intersect([v1x(:) v1y(:)],[v2x v2y],'rows')
ix =
1
5
>>

3 Comments

NB: This works for the two end cases as shown in the example data; however, it'll likely fail if you try to find the general case of the intermediates unless the two sets of nonintegers are generated by the identical process. For example, consider
>> V1x=linspace(1,10,91); % another way to write v1x
>> all(V1x==v1x) % oops, similar but not identical!!!
ans =
0
>> sum(V1x==v1x) % how many of the 91 are the same?
ans =
76
>> max(abs(V1x-v1x)) % how much difference is there???
ans =
8.8818e-16
>>
OK, not much, but enough exact equality test will fail. Moral: Be forewarned!!!
Thanks for the help,
I got your original method to work on my full data set, but had to round my values to remove the errors as you point out.
Cheers
Gareth
Ayup; point of the illustrative comment. Try the new ismembertol if you have R2015X; TMW hasn't yet implemented such with the remaining functions afaik. I'da wished for adding a 'tolerance' optional argument to the originals rather than another whole set of function names to add to the namespace pollution, however, but they are a need facility.

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Improving Code in Help Center and File Exchange

Products

Asked:

on 26 Oct 2015

Commented:

dpb
on 27 Oct 2015

Community Treasure Hunt

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

Start Hunting!