Closest number problem in m-file
Mostrar comentarios más antiguos
I need more help..
I have script looks like this
A = [2 5 9 15];
f = input('Enter input : ');
[val, idx]=sort(abs(f-A));
[val, idx]=min(abs(f-A));
A(idx)
If the input I enter is 3
Of course the output of A(idx) was 2
But I want the value output are 5 (the second closest of 3).
As well as for different inputs.
Please help me..
Thanks before
Respuestas (1)
KL
el 12 de Mayo de 2017
[val, idx]=sort(abs(f-A));
A(idx(2))
3 comentarios
Guillaume
el 12 de Mayo de 2017
For the first line, even better if you're not using val:
[~, idx] = sort(abs(f-A));
which makes it clear that you only want the indices.
Yuli Hartini
el 12 de Mayo de 2017
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!