closest value to zero excluding the first one?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
out of our list of stuff to search for
?
v = [-0.1 2 4 -0.5 8];
[~,i] = min(abs(v(2:end)))
v(i+1)
3 Kommentare
Image Analyst
am 12 Jan. 2023
"out of our list of stuff to search for" <= He's searching FOR the index, so I think he really meant "out of our list of stuff to search", which is basically what @Torsten did. (No problem - non native English speakers probably don't know subtleties like that) The index would be
index = i+1;
Ali Almakhmari
am 12 Jan. 2023
Bearbeitet: Ali Almakhmari
am 12 Jan. 2023
Ali Almakhmari
am 12 Jan. 2023
Bora Eryilmaz
am 12 Jan. 2023
Bearbeitet: Bora Eryilmaz
am 12 Jan. 2023
load('var.mat')
[~,I] = sort(abs(dis_y), 'ascend');
% Closest value to 0:
dis_y(I(1))
% Second closest value to 0:
dis_y(I(2))
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!