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

1 view (last 30 days)
Tawsif Mostafiz
Tawsif Mostafiz on 5 Jul 2021
Edited: KSSV on 5 Jul 2021
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

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by