Clear Filters
Clear Filters

Closest number problem in m-file

1 view (last 30 days)
Yuli Hartini
Yuli Hartini on 12 May 2017
Commented: Yuli Hartini on 12 May 2017
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

Answers (1)

KL
KL on 12 May 2017
[val, idx]=sort(abs(f-A));
A(idx(2))
  3 Comments
KL
KL on 12 May 2017
Edited: KL on 12 May 2017
true, I just copy pasted her code
Yuli Hartini
Yuli Hartini on 12 May 2017
Thank you guys..
This is very helpful.

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!