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

2 次查看（过去 30 天）
Brianna Miranda2022-10-28

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

### 采纳的回答

Chunru 2022-10-28

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

### 类别

Find more on Matrices and Arrays 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!

Translated by