MATLAB Answers


Get multiple indices coinciding closest to a certain value

Asked by Samuel
on 4 Jul 2013

Hello all,

I have an x by 2 array- it is a freq vs amplitude graph. The graph is similar in shape to a typical ideal frequency curve (going uphill to a peak point then downhill).

I am trying to write a code that will detect the half power of the amplitude, and display the frequency of the two half power locations(since it goes up and down).

With some research(stackoverflow forums), I was able to devise a code utilizing the minimum difference between the value and half power value, but this code approach only can get me the first half power point- any help in getting the second one as well will be of great help. Thanks!

the code portion is below:

%halfpow is the number I want to be searched for
%v1 is the column vector that contains the amplitudes  
[ind ind]=min(val);


2 Answers

Answer by Guru
on 4 Jul 2013
 Accepted answer

The code you have is fine, what you have are two minima values that you are seeking. So by breaking up your search points you can find it easier. What you

va1 = abs(v1-halfpow); % This part is exactly what you want like above
[C,ind1] = min(val);
point1 = v1(ind1)
% Remove the data points that you just found the minimum of to find another
val(ind-2:ind+2) = max(val);
[C,ind2] = min(val);
point2 = v1(ind2)

This should give you the idea. The concept is min finds the first global minimum of the vector. To find a second minimum, you need to ensure the first minimum is no longer a minimum.


  1 Comment

on 4 Jul 2013

thanks for the descriptive solution. I actually ended up using the hint from the cyclist to figure out the answer, but your code seems to make more sense (and is a lot shorter as well compared to mine).

Answer by the cyclist
on 4 Jul 2013

You could use sort(val) rather than min(val), and you will get the sorted values, and the indices to them.

  1 Comment

on 4 Jul 2013

Thanks for the suggestion- it really helped me in figuring out the code. However, I will have to give the upper hand to the answer below, due to its much more descriptive content.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!