I am facing a problem with ismember subfunciton in Matlab.
Show older comments
I am facing a problem with ismember subfunciton in Matlab.
UseCase:
Simulation_Time_Vecort = 0:0.01:42;
TestVector =
0 1.0000
0.7000 0
0.8000 1.0000
7.3100 0
7.4100 1.0000
13.6200 1.0000
20.3200 1.0000
27.0200 1.0000
33.0200 1.0000
39.9200 1.0000
Simulation_Values_Vecort = zeros(1,length(Simulation_Time_Vecort));
FlagSignalName_Value = [Simulation_Values_Vecort ; Simulation_Values_Vecort]';
FlagSignalName_Value(:,1) = double(Simulation_Time_Vecort);
FlagSignalName_Value(:,2) = ismember(Simulation_Time_Vecort,TestVector_Data(:,1)');
Problem: When I try to excute this script, ismember is not executed as expected. For some cases though the value exist in both vectors used in ismember function states above, output is not displayed or updated as value 1. In my case for value ‘0.7000’ ismember result is displayed as 0
Answers (1)
Walter Roberson
on 6 Mar 2017
0 votes
This is expected. http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
If you have a new enough MATLAB version, use ismembertol
5 Comments
"If you have a new enough MATLAB version, use ismembertol"
Alternatively, round all values to 2 decimals, since the data shown does not appear to have more precision
FlagSignalName_Value(:,2) = ismember(round(Simulation_Time_Vecort, 2), round(TestVector_Data(:,1)', 2));
edit: as pointed out by Walter, due to the way floating point works, there's no guarantee that
round(somevaluenearly0.7, x)
and
round(someothervaluenearly0.7, x)
will produce the exact same result. With x = 2, I'm hoping it shouldn't be the case (but I haven't tested it)
Walter Roberson
on 6 Mar 2017
Ah, but the rounding could come out differently.
Guillaume
on 6 Mar 2017
To two decimal place? Hopefully not. Otherwise, I'd say that round is badly implemented.
>> A = 0.996;
>> B = 0.994;
>> round2sf(A,2)
ans = 1.0000
>> round2sf(B,2)
ans = 0.99000
The values differ by only 0.002, but rounding to 2 sigfig will make them diverge, not converge. Using a tolerance would resolve this. All pairs of values that differ by a little less than least significant figure and that are on the rounding boundary will diverge...
Guillaume
on 6 Mar 2017
Ah yes, but your values differ on the 3rd significant digit. From the example data provided, I'm assuming that the values only differ by a few eps.
Yes, tolerance comparison is more robust. My suggestion was for if you don't have ismembertol.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!