How to allow user to retrieve indices for particular value in an array, not the value itself?

조회 수: 2 (최근 30일)
I want to allow a user to index for a value closest to their input value within a table.
I have an Faxis, which I created from performing an FFT, so this does not have rounded values. I would like the user to be able to input a frequency, say 1000Hz and then for the program to locate the nearest frequency in the array.
As the program needs to use the index and not the value itself at a later point, how can I get this, as I can only see people finding the actual value on here?
Faxis = linspace(0,20e3,30e3) %For example only
Frequency_Index = 1000;
Index = find(min(abs(faxis-Frequency_Index))
The closest I have seen is using the find function but even then it seems to just retrieve the value itself, which is of no use here.

채택된 답변

Dennis
Dennis 2018년 11월 9일
In your example you use the find function on only 1 value ( min() returns 1).
You were close, min() can actually return the index on its own.
[~,idx]=min(abs(faxis-Frequency_Index))

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by