Issue with finding intersection between randomly generated matrices

2 visualizzazioni (ultimi 30 giorni)
I have two matrices randomly generated A and A_new (subset of A). When I use the intersect command, very surprisingly it negates one of the rows, while identifies the other four rows. Below is the screenshot while trying to debug the issue:
However, when I copy A and A_new directly from the workspace into different variables (A1 and A2 respectively) and use the same intersect command, the result comes out perfect:
A1 = [-17.1747990884382 19.2719049157323 99.6315612404934 % note: A is actually 71-by-3, but here I used only 22-by-3
-10.1200347628603 44.6569202438877 94.7227267967226
58.6115851834664 -9.53770294596646 92.9138476931620
47.0203961819196 -18.8814327228311 87.4170841370532
93.0719877076277 -40.6092086918209 85.2369720823465
62.7067365154531 -40.8222018404139 82.3859521001850
28.4948014516781 24.4091032205797 80.3478942902817
-27.4120788164787 46.4475836126667 80.0165302698221
52.5520137564189 -86.2238156283654 78.0805162946764
34.8647540565998 -78.4903445824848 73.9133775177247
64.6182025041414 -8.02799812625432 71.0037105461384
-74.2010206861346 27.4023369519983 70.4017187841455
13.4165633024713 43.2557370559446 70.1735863347631
79.3294488296403 46.5421363760066 67.5796378292223
71.7027257281106 -99.6206397947143 66.7042052990168
8.66568815370449 86.0343589130021 59.3423113221334
25.6901433887827 -13.4695262062841 58.4318188330688
-39.5611117917563 47.4462315581845 53.9216963646236
-8.49830028866433 -46.7213246886748 49.1592271029911
-74.0052720695440 99.2224561788284 49.0989168204624
-43.0371001304189 58.8076794390692 46.2077865058290
78.7271494784465 95.8905249403584 45.4459816955421];
A2 = [93.0719877076277 -40.6092086918209 85.2369720823465
79.3294488296403 46.5421363760066 67.5796378292223
8.66568815370449 86.0343589130021 59.3423113221334
-74.0052720695440 99.2224561788284 49.0989168204624
78.7271494784465 95.8905249403584 45.4459816955421];
[~,idx2,~] = intersect(A1,A2,'rows');
idx2 =
20
16
22
14
5
I wonder why this discrepency? I thought of using rounding option and it works for this random set, however, messes up some other random sets. Any suggestion to fix the issue would be highly appreciated. I am really confused.
Thanks in advance, and sorry for the long question.

Risposta accettata

dpb
dpb il 26 Giu 2020
The results for intersect are correct ..
>> (A(16,:)-A_new(3,:))
ans =
1.0e-13 *
0 0 0.1421
>>
The other data arrays were apparenty constructed by taking fewer significant digits.
Testing floating point for exact equality is always fraught with such issues--if you want matching with eps() of the values or other tolerance and not need exact match to the last bit, use ismembertol
You have to look at the value as stored in memory to make a judgement, you can't just compare the visual representation on the screen.
  2 Commenti
J AI
J AI il 26 Giu 2020
Thanks a lot - it works now with ismembertol. By the way, I thought workspace stored the actual values and did not have any more decimals places beyond what it shows. However, learned something new about MATLAB today. Thanks once again for your help!
dpb
dpb il 26 Giu 2020
Read the information on display formatting at the format command doc page to see what precision is displayed for various choices of the command window format.
Also be aware that how you generate a floating point value can cause rounding differences owing to whether the result is read in or calculated and things like order of calculations can result in two variables not being identical to the LSB of mantissa and so cause failure to match equality tests.
The default output precision for builtins such as csvwrite are set to use a shorter precision by default so that is another way that one can introduce a discrepancy.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by