Why maximum value of array is not present in original array?
Afficher commentaires plus anciens
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)?
Réponses (1)
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 commentaires
DGM
le 5 Juil 2021
parentheses were misplaced
idx=find(abs(arr- maximum)<= tol);
Tawsif Mostafiz
le 5 Juil 2021
KSSV
le 5 Juil 2021
clc; clear all ;
arr = [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];
[val1,idx1] = max(arr) ;
idx2 = find(abs(arr-val1)<10^-3) ;
[idx1 idx2]
Tawsif Mostafiz
le 5 Juil 2021
Walter Roberson
le 5 Juil 2021
Modifié(e) : KSSV
le 5 Juil 2021
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!