result of ismember is wrong in my code correct in command window

Dear, I have this code, when I get the result from the loop I find the common rows between pp and pointsout but I got wrong result, when I copy my matrix pp and pointsout to command window or I comment my code and tried from file it gives correct answer. for example
pp =[
29.6360 20.2328 25.8546
22.0039 27.8829 29.8251
23.3343 26.8422 20.0174
20.1460 29.4970 29.2674
27.7054 29.9161 24.0945
29.9539 27.4832 29.3899];
pout=[];
pin=[];
points=pp;
for k=1:size(pp,1)
[ClosestPoint,Distance] = Cl(XYZ,pp(k,:));
if Distance ==-2
pin=[pin; pp(k,:)]
end
pout=[pout; ClosestPoint];
end
pointsout=[pointsin; pout];
% find the index of the in points
idx=ismember(pointsout ,points,'rows')
if
pointsout =[
33.6612 39.8834 33.3147
25.2505 41.9976 32.2839
25.6122 41.9067 32.3282
23.0877 42.5413 32.0188
29.4130 40.9513 32.7941
32.6676 40.1332 33.1929];
it gives me just five rows by ismemebr, when I take the two matrices and check in the command window I get 6 rows even when I comment the code to check. What is wrong of the code, why it effect the result. I'm sorry I can't write the whole code for respect to function Cl its 4 files. Thanks for any comments and help.
Nadia,

 Accepted Answer

I suspect this is just floating point issues. Reading from a file may read in all of the precision of the numbers (i.e., precision beyond the 4 digits after the decimal point you are showing). But setting the numbers directly from the command line as in your code above may give you slightly different results. I suggest you check and compare the pp and pointsout etc you get from the various methods you are using to see if there are any differences.

7 Comments

Dear James,
thanks for replying quickly.
there is no different in the decimal points, its the same. I suspect in my code, do think the way that I wrote the loop and the result may effect because as I said when I comment the code in my fiel and apply ismember for the matrices its ok.
regards
Nadia Nadi: it is very likely that it is floating-point issues, exactly as James Tursa replied. You should download this:
and compare the values that you think are identical. Most likely they are not. You will learn that what is printed in the command window is not the same as the value stored in memory, it is only a very simplified version of of the value.
Dear Stephen,
I think that's correct look at this example, these matrices give me one similar row in the command and from my file, I used format long and short and both of them give me incorrect result. If I want to find ismember just for 4 digits 26.0140 what should I do ?, because format can not help. I'm searching for some ideas know, I wrote single (PP), but it gives the same so is better to use 4 digits, so I used
PP=round(PP,5)
its the same, any idea that you can help me please? regards,
Nadia
You have two options to fix the problem:
  1. use ismembertol introduced in r2015a. I suspect that ismembertol is significantly slower than standard ismember but for small sets, it probably doesn't matter
  2. round the values before passing them to ismember. Should be faster (and simpler) than option 1:
idx = ismember(round(pointsout, 4) ,round(points, 4),'rows')
Dear Guillaume,
thanks that is great, this works well in r2015a, but not in r2012b, because round(points, 4) does not work in it. Do you know a way to use it in r2012b. that will be better because all my files run from it.
regards
You can emulate the most recent round function with this one:
roundn = @(x,n) round(x .* 10.^n)./10.^n; % Round ‘x’ To ‘n’ Digits
Thank you very much, it was all good advice. I appreciate all of you.

Sign in to comment.

More Answers (0)

Asked:

on 3 Dec 2015

Edited:

on 5 Dec 2015

Community Treasure Hunt

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

Start Hunting!