How to allow user to retrieve indices for particular value in an array, not the value itself?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dennis
on 9 Nov 2018
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 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!