Reputation: 8260
I've got some numerical data of a positive function with a number of "zeros", however, none of these zeros is ever exactly zero. I'd like to extract the positions of all these zero values, and was wondering what the best way to do it was. Here's a contrived example that is similar to what I'd like to solve, with an illustrative plot of the values, the adjacent differences, and the signs of those differences:
a = [ 0.0062 ; 0.0041 ; 0.0021 ; 0.0003 ; 0.0015 ; 0.0031 ; 0.0045 ; 0.0059 ; 0.0062 ; 0.0041 ; 0.0021 ; 0.0003 ; 0.0015 ; 0.0031 ; 0.0045 ; 0.0059 ]/0.0062 ;
d = diff(a) ;
r = -3/2:0.5:length(a)/2-4/2 ;
close all ;
hold on ;
plot( r, a ) ;
plot( r(1:length(d)), d ) ;
plot( r(1:length(d)), sign(d) ) ;
Which produces:
Based on what I've done, I could iterate over the array and find all the places that the sign of the difference array changes from -1, to 1. Two questions:
Upvotes: 2
Views: 867
Reputation: 112699
To find values at which the difference changes from positive to negative:
indices = find(diff(sign(diff(a)))==2)+1;
In your example this gives
indices =
4
12
Depending on what you want to achieve, you could find the closest-to-zero value(s) directly as follows:
b = abs(a);
indices = find(b==min(b))
which also gives
indices =
4
12
Note that this will find the index of the value that is closest to zero, unless there is a tie between several values, in which case it returns several indices.
Upvotes: 4
Reputation: 1172
What about take your_data
, do 1./your_data
and then use findpeaks
. See the documentation for more options on findpeaks
including the possibility to give treshold value for lowest peak to find. It should help.
Upvotes: 0