How to allow user to retrieve indices for particular value in an array, not the value itself?
1 次查看(过去 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!