closest value to zero excluding the first one?

10 visualizaciones (últimos 30 días)
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
Comentada: Bruno Luong el 12 de En. de 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 comentarios
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
I am attaching an example variable and the answer your code should result in should be 175 or the 174 index.
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
I = find(diff(dis_y>=0),1);

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (2)

Torsten
Torsten el 12 de En. de 2023
Editada: Torsten el 12 de En. de 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
  3 comentarios
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
Editada: Ali Almakhmari el 12 de En. de 2023
hmm I dont think this works. For example, I attached this variable here. The answer to my question should be 175 or the 174 index. But with this code you have I do not get that.
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
Actually I found the solution: I = find(diff(dis_y>=0),1);

Iniciar sesión para comentar.


Bora Eryilmaz
Bora Eryilmaz el 12 de En. de 2023
Editada: Bora Eryilmaz el 12 de En. de 2023
load('var.mat')
[~,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 comentarios
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
This is still not giving me 175 or 174.
Ali Almakhmari
Ali Almakhmari el 12 de En. de 2023
I = find(diff(dis_y>=0),1);

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by