if statement returns false to checking if 0.05 +0.01 = 0.06

6 views (last 30 days)
Hi Everyone,
I'm trying to write a .m script that would determine if points in an array are within 0.01 of each other (i.e. 0,0.01,0.02,0.03) which would indicate the points are 'adjacent', as opposed to (0,86,0.98, etc). To do that I ask over the size of the array, is this element + 0.01 = the next element (as seen below in the code). If that's true, do some stuff. However if you check the print out of the data (under Sample date) it checks 0.05 and finds it is not within 0.01 of 0.06 (returns 0 also known as false)!
This is such a basic logic problem I don't know where to start. Anyone have any idea what's going on?
for j = 1 : 1 : ij
if j<ij
lin_SOC(j)
lin_SOC(j)+0.01==lin_SOC(j+1)
if lin_SOC(j)+0.01==lin_SOC(j+1) %if this is true then points are ajacent (i.e. in same region)
if region_size==0 % if region size is 0 we're at the beginning of a new linear region
lin_start_point =[lin_start_point;lin_SOC(j)]
region_size
end
region_size=region_size+1;
else
if region_size~=0 % if region size is 0 we're at the beginiong of a new linear region
lin_end_point =[lin_end_point;lin_SOC(j)]
region_size
end
num_regions=num_regions+1; %number of non-linear regio
region_size=0; %reset nonlinear region size counter
end
end
end
~~Sample Data~~
lin_SOC=
0.0500
ans =
0
lin_SOC =
0.0600
ans =
0
lin_SOC =
0.0700
ans =
1
lin_start_point =
0.0700
region_size =
0
lin_SOC =
0.0800
ans =
1
lin_SOC =
0.0900
ans =
0

Accepted Answer

Stephen23
Stephen23 on 6 Dec 2015
Edited: Stephen23 on 6 Dec 2015

More Answers (1)

Walter Roberson
Walter Roberson on 6 Dec 2015

Categories

Find more on Loops and Conditional Statements 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!