How to find the maximum value between two indices in an array?

24 views (last 30 days)
I am trying to find the maximum power spectrum value between two indices in an array that correspond to specific frequencies in another array. mode2a and mode2b are the frequency range that I want to find the maximum power spectrum value for. In my code all of the lines give me the correct outputs except for the last two lines where I try to output the frequency value that corresponds to the maximum power spectrum. Can anyone see what is causing the last two lines to return incorrect values?
mode2a = 200; % frequency lower bound (Hz)
mode2b = 600; % frequency upper bound (Hz)
indexA = find(f_data==mode2a); % index that corresponds to frequency lower bound
indexB = find(f_data==mode2b); % index that corresponds to frequency upper bound
[amp,indMax] = max(P1_data(indexA:indexB)); % find maximum value within the indexA-indexB range and its index
freq = f_data(indMax); % compute the frequency that corresponds to the index of the max value of the power spectrum

Accepted Answer

Chunru
Chunru on 28 Oct 2022
Edited: Chunru on 28 Oct 2022
Without yor data and complete code, this is a guess:
mode2a = 200; % frequency lower bound (Hz)
mode2b = 600; % frequency upper bound (Hz)
indexA = find(f_data>=mode2a, 1, 'first'); % index that corresponds to frequency lower bound
indexB = find(f_data<=mode2b, 1, 'last'); % index that corresponds to frequency upper bound
[amp,indMax] = max(P1_data(indexA:indexB)); % find maximum value within the indexA-indexB range and its index
freq = f_data(indexA-1+indMax); % compute the frequency that corresponds to the index of the max value of the power spectrum

More Answers (0)

Community Treasure Hunt

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

Start Hunting!