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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!