Why maximum value of array is not present in original array?

1 view (last 30 days)
In this code:
arr(p)=abs(c); %absolute value of c
if(arr(p)<1)
if(arr(p)>0.9)
array(t)=white1;
cor(t)=arr(p); %correlation for c>.95 && c<1
t=t+1;
end
end
maximum=max(cor);
when I do this:
x=find(arr == maximum, 1);
It works for some input. It finds the first index of arr==maximum. However, when I print maximum with fprintf, for some input the x shows blank, such as:
x is
To investivage the problem, I printed arr(p) too. And found that the maximum I got from maximum=max(cor); is not present in the arr(p)
Let me give you the output:
Here's arr(p) in output:
4.595946e-02
2.556745e-02
6.488689e-02
6.076814e-02
8.913487e-03
6.457960e-02
1.031540e-01
1.213301e-02
5.870144e-02
1.425681e-01
1.397469e-03
1.525214e-01
1
5.967187e-02
7.100746e-02
7.474598e-02
1.066773e-01
1.259531e-01
7.464354e-01
7.420288e-01
7.217714e-01
4.300225e-01
3.690350e-01
3.326089e-01
3.141340e-01
1.852171e-01
1.726543e-01
1.494046e-01
1.428864e-01
1.347272e-01
1.168218e-01
1.118371e-01
9.120090e-02
8.713045e-02
5.995163e-02
6.033924e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
And here's maximum:
maximum is 9.682138e-01
As you can see, maximum does not match any arr(p) values.
But when I print cor(t), the value of maximum can be found in it. Among other values.
What I don't understand is that maximum is the maximum value of arr(p) when arr(p)>0.95 and <1, which is stored in cor(t). But when I do max(cor), why maximum value is not present in arr(p)?

Answers (1)

KSSV
KSSV on 5 Jul 2021
Edited: KSSV on 5 Jul 2021
To understand this you should read about floating point numbers. You should not use == to equare such numbers. You need to set a small tolerance and check the absolute of difference.
tol = 10^-5 ;
idx=find(abs(arr- maximum))<= tol;
Read more:
  5 Comments
Tawsif Mostafiz
Tawsif Mostafiz on 5 Jul 2021
@KSSV I want to find max(cor) instead of max(arr). And when I do this:
for k = 1 : length(theFiles) % files are alreaady defined in code
p=0;
t=0;
for q=.1:20
if(arr(p)<1)
if(arr(p)>0.85)
cor(t)=arr(p); %cor(t) already defined in code
t=t+1;
end
end
p=p+1;
end %end of q loop
[val1,idx1] = max(cor) ;
idx2 = find(abs(arr-val1)<10^-3) ;
end % end of k loop
Again, it shows idx2 for some of the inputs when printed, but for some it shows blank. such as:
idx2 is
I don't know whether it is relevent information or not, but the file for which the output above is shown has the minimum of all the val1 of other files, such as, the values of val1 for other files are as follows:
9.554158e-01
9.634148e-01
9.750799e-01
9.841079e-01
9.753829e-01
9.764703e-01
Where the value for blank idx2, the val1 is:
9.243360e-01
How do I fix it?

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!