필터 지우기
필터 지우기

How to find the second closest number?

조회 수: 7 (최근 30일)
Yuli Hartini
Yuli Hartini 2017년 1월 5일
답변: KSSV 2017년 1월 5일
I have a matrix looks like this:
A = [1990;1993;1998;2001;2004 ]
And I would like to find the closest value to f=1999 (in this case the first closest value is certainly 1998), but how can I find the second closest value (2001) after I found the first value.
Thanks before
  댓글 수: 1
David Goodmanson
David Goodmanson 2017년 1월 5일
Hi Yuli, if you take abs(A-your_number) and sort it, you will get the entire list of distances. The sort command also has a second output that will show you the location of each one in the unsorted array.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2017년 1월 5일
clc; clear all ;
A = [1990;1993;1998;2001;2004 ] ;
f = 1999 ;
[val,idx] = sort(abs(f-A)) ;
A(idx)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by