필터 지우기
필터 지우기

Finding the index value corresponding to a value closest to 0 in an array

조회 수: 162 (최근 30일)
Hi,
I have an array with x amount of values. How can I find the index value of the element that is closest or equal to a certain value?
I tried it in the following manner, but it doesn't work when the value of the element in Temp is equal to the RefTemp value.
Temp = [-15.3, 0.2, 15.2, 30, 45.3];
RefTemp = 30; %Value to compare the Temp array values to
for ii = 1:length(Temp)
TempCalc(ii) = abs(Temp(ii) - RefTemp);
end
find(min(TempCalc));
Thank you very much for your help!

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 1월 30일
Do you have the Stats toolbox? if you do then do a nearest neighbor search as follows:
location = knnsearch(Temp',30);
If you don't have stats toolbox then use delauny to do nn search:
>> tri = delaunayn(Temp');
>> dsearchn(Temp',tri,30)
ans =
4
  댓글 수: 2
Mihai
Mihai 2013년 1월 30일
Interesting! I don't have the stats toolbox, and I've never seen either of those 2 functions before. It seems simple enough. I briefly tried playing around with the delaunayn function, and it seems it wouldn't work if 2 elements in the array were equal. This isn't a problem for me since the elements in my Temp array will be unique.
Shashank Prasanna
Shashank Prasanna 2013년 1월 30일
Nearest neighbor computation comes up in engineering and science more often then you can imagine, from pdf estimation to clustering. There are advanced data structures such as kdtrees which speed up neighbor search for higher dimension data. Also knnsearch has options on what to do during a tie.

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

추가 답변 (4개)

Cedric
Cedric 2013년 1월 30일
편집: Cedric 2013년 1월 30일
You have several options. The first question is: do you really need the index, or could you use a vector of logicals, e.g. for indexing something else. Look at the following; we want to extract all volumes associated with temperatures that are closest to a ref value:
>> temp = [-15.3, 0.2, 15.2, 30, 45.3];
>> volume = [4, 7, 28, 35, 20] ;
>> ref = 27.2 ;
>> dif = abs(temp-ref)
dif =
42.5000 27.0000 12.0000 2.8000 18.1000
>> min(dif)
ans =
2.8000
>> match = dif == min(dif)
match =
0 0 0 1 0 % Vector of logicals indicate
% where dif equals its min.
>> idx = find(dif == min(dif))
idx =
4 % Index of element that
% the min matches
Now you can extract the corresponding volume with either the vector of logicals (which avoids using find()) or the index.
>> volume(match)
ans =
35
>> volume(idx)
ans =
35
This is one "vector" way to achieve what you want; without all the extra steps, this reduces to:
>> dif = abs(temp-ref) ;
>> volume(dif == min(dif))
ans =
35
  댓글 수: 2
Mihai
Mihai 2013년 1월 30일
Thanks! I think the first part of your answer is along the same route I was going for and does what I need. I have to find the index value to use it with other arrays that correspond to the Temp array.
Satish Tadepalli
Satish Tadepalli 2016년 11월 11일
awesome explanation @ Cedric Wannaz.

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


Kasper Ornstein-Mecklenburg
Kasper Ornstein-Mecklenburg 2017년 10월 24일
편집: Kasper Ornstein-Mecklenburg 2017년 10월 24일
The min function returns index as second output.
>> a = [-15.3, 0.2, 15.2, 30, 45.3]; %Define vector to analyze
>> aRef = 30; %Set reference
>> aDiff = abs(a - aRef); %Calculate diff
>> [minVal, minInd] = min(aDiff); %Find value closest to aRef
>> a(minInd)
ans =
30

rawand
rawand 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
simply:
Temp = [-15.3, 0.2, 15.2, 30, 45.3];
RefTemp = 30; %Value to compare the Temp array values to
for ii = 1:length(Temp)
TempCalc(ii) = abs(Temp(ii) - RefTemp);
end
find(TempCalc == min(TempCalc));
  댓글 수: 1
Stephen23
Stephen23 2016년 7월 21일
편집: Stephen23 2016년 11월 12일
@rawand: there is absolutely no point in wasting time writing slow and ugly loops as if this were some low-level language like C. MATLAB is a high-level language, and vectorized code is faster and neater:
>> TempCalc = abs(Temp-RefTemp);
>> find(TempCalc == min(TempCalc))
ans =
4

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


Robert Brandalik
Robert Brandalik 2017년 3월 17일
Also an Option:
unique([find(Temp-RefTemp == min(complex(Temp-RefTemp))),find(RefTemp-Temp == min(complex(RefTemp-Temp)))])

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by