How to determine the two closest values to a threshold value

6 visualizaciones (últimos 30 días)
Jonathan Moorman el 11 de Ag. de 2020
Comentada: hosein Javan el 11 de Ag. de 2020
Hi! I have a threshold value of 0.9 and I need to find its two closest values in a matrix.
Below is a simple example of what my question is based on. Is there a way to determine the previous y value before 0.9 (0.88 in the example) and the next value after (0.92)?
y = [0, 0.2 , 0.6, 0.88, 0.92, 1]
(Note* this is only an example matrix. The matrix values will be different every run)
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Respuesta aceptada

hosein Javan el 11 de Ag. de 2020
Editada: hosein Javan el 11 de Ag. de 2020
%% nearest value considerin it is bigger or smaller than threshold
m1 = min(y(y>0.9)) % nearest value after threshold
m2 = max(y(y<0.9)) % nearest value before threshold
%% nearest value regardless of being bigger or smaller than threshold
[~,i] = sort(abs(y-0.9));
m1 = y(i(1)) % 1st nearest value
m2 = y(i(2)) % 2nd nearest value
2 comentariosMostrar NingunoOcultar Ninguno
Jonathan Moorman el 11 de Ag. de 2020
Worked great. Thanks!
hosein Javan el 11 de Ag. de 2020
you're welcome.

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by