intersect misses common elements of 2 matrices

2 ビュー (過去 30 日間)
Jack
Jack 2016 年 12 月 16 日
編集済み: James Tursa 2016 年 12 月 17 日
I've been using intersect to identify the common elements of two 2D grids. However, when I try it against the following code, it misses some of the common elements.
[x1Grid_1,x2Grid_1] = meshgrid(-70:0.5:70, 1:0.05:17);
xGrid_1 = [x1Grid_1(:), x2Grid_1(:)];
[x1Grid_2,x2Grid_2] = meshgrid(-56:0.5:35, 6:0.05:16);
xGrid_2 = [x1Grid_2(:), x2Grid_2(:)];
[c1,c2,c3] = intersect(xGrid_1,xGrid_2,'rows');
The vector xGrid_2 should be made entirely of elements from xGrid_1. xGrid_2 has 36783 rows, but after running intersect, vectors c1-c3 only have 30195 elements, meaning they miss some of the common elements. I can't understand why this would be happening. Any input would be helpful.
I'm using 2016a.
Thanks, Jack
  1 件のコメント
John BG
John BG 2016 年 12 月 16 日
what are you after, the line intersection of 2 planes?

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

採用された回答

Image Analyst
Image Analyst 2016 年 12 月 16 日
I think this might be a case of floating point numbers, arrived at via different operations, not matching when you think they should. You need to check within a tolerance. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  3 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 16 日
True, as are all negative powers of 2: 2^(-1) and 2^(-2), etc. (0.5, 0.25, 0.125, etc.).
James Tursa
James Tursa 2016 年 12 月 17 日
編集済み: James Tursa 2016 年 12 月 17 日
Or if you are on a Windows system where the underlying sprintf algorithm is different, you can use num2strexact instead:
>> num2strexact(0.05)
ans =
5.000000000000000277555756156289135105907917022705078125e-2

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

その他の回答 (1 件)

Jack
Jack 2016 年 12 月 16 日
Rounding it did the trick - I just used
xGrid_1 = round(xGrid_1*100)/100;
xGrid_2 = round(xGrid_2*100)/100;
to cutoff the decimals after 0.01. That made it work.
Thanks

カテゴリ

Help Center および File ExchangeEncryption / Cryptography についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by