# closest value to zero excluding the first one?

2 views (last 30 days)
Ali Almakhmari on 12 Jan 2023
Commented: Bruno Luong on 12 Jan 2023
If given a long vector of values. How can I find the index of the closest value to zero excluding the first value of the vector out of our list of stuff to search for
##### 2 CommentsShow 1 older commentHide 1 older comment
Ali Almakhmari on 12 Jan 2023
I = find(diff(dis_y>=0),1);

Ali Almakhmari on 12 Jan 2023
"dis_y" being the variable:
I = find(diff(dis_y>=0),1);
Bruno Luong on 12 Jan 2023
That is NOT your question asks for, that is first consecutive points where 0 is cross from positive to negative.

Torsten on 12 Jan 2023
Edited: Torsten on 12 Jan 2023
out of our list of stuff to search for
?
v = [-0.1 2 4 -0.5 8];
[~,i] = min(abs(v(2:end)))
i = 3
v(i+1)
ans = -0.5000
Ali Almakhmari on 12 Jan 2023
Actually I found the solution: I = find(diff(dis_y>=0),1);

Bora Eryilmaz on 12 Jan 2023
Edited: Bora Eryilmaz on 12 Jan 2023
[~,I] = sort(abs(dis_y), 'ascend');
% Closest value to 0:
dis_y(I(1))
ans = 2.3761e-05
% Second closest value to 0:
dis_y(I(2))
ans = -0.0014
##### 2 CommentsShow 1 older commentHide 1 older comment
Ali Almakhmari on 12 Jan 2023
I = find(diff(dis_y>=0),1);

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!