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

 採用された回答

dpb
dpb 2015 年 10 月 26 日

0 投票

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

3 件のコメント

dpb
dpb 2015 年 10 月 27 日
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!!!
Gareth Jones
Gareth Jones 2015 年 10 月 27 日
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
dpb
dpb 2015 年 10 月 27 日
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.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Debugging and Improving Code についてさらに検索

製品

タグ

質問済み:

2015 年 10 月 26 日

コメント済み:

dpb
2015 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by