Find maximum values and their range and remove them to find the next maximum values

1 Ansicht (letzte 30 Tage)
I am trying to write a code that finds maximum value and its range. However, I want the code to find the next maximum value but the previous maximum value is removed or set to zero. This is what I got so far.
x = Data(:, 1); % import Y of column 1 from Data
B = zeros(1,100000000); % pre-allocate B
for n = 1:500
[M,I] = max (x);
B(n) = M;
x(I) = -Inf;
if M > 2 % set threshold
c = x(I-299 : I+700); % range of data points to be taken from x
c = c'; % transpose c
pulse(n,:) = c; % n-th row of pulse
end
end
The code does not remove the maximum value and its range. I have tried putting c = 0 and x(I-299:I+700) = 0 but they don't work. And I am still unsure where do I have to insert it.

Antworten (1)

Guillaume
Guillaume am 22 Nov. 2016
I don't really understand the question. Your code does replace the maximum value by -Inf at each step of the loop, which seems to be what you want. I don't understand what you mean by range of the max. The max is just one number.
There are a few things that don't look right with your code in any case:
1) Why are you creating such a large B when your loop only fills in the first 500 values?
2) x(I-299 : I+700) is going to result in an error if the max is found within the first 299 elements or the last 700 elements.
c = x(max(1, I-299) : min(end, I+700));
is a lot safer. However, it may create shorter c. So, maybe:
c = [nan(1, max(0, 300-I)), x(max(1, I-299) : min(end, I+700)), nan(1, max(0, 700+I-numel(x))];
3) the transposition in completely unnecessary. Matlab will reshape c into a row vector anyway because it is assigned to a row vector
%c = c'; %NOT NEEDED
pulse(n, :) = c;
4) if the max is less than 2 you don't assign anything to the corresponding row of pulse. If a latter row receives something the non-assigned rows will be set to 0. If no latter row receives anything, your pulse matrices will have less than 500 rows. Is that intended? If not, it would be safer to preallocate pulse
pulse = zeros(500, 1000);
  3 Kommentare
Guillaume
Guillaume am 23 Nov. 2016
Regarding the two points:
2. I understood perfectly well what it is doing. As said, if the maximum is found within the first 300 points (I < 300) this will result in error "Subscript indices must either be real positive integers or logicals", and if it is found within the last 700 points (I>numel(x)-700) this will result in error "Index exceeds matrix dimensions".
4. Really? Your pulse matrix will have an arbitrary number of rows (up to 500). That numer of rows only depends on which step of the for loop was the last maximum above M found. pulse will also contain arbitrary rows that are just 0. To me, that's just inviting bugs later on in the code.
As Image Analyst suggests, findpeaks with the 'MinPeakDistance', 1000 and the 'MinPeakHeight', 2 options will do more or less what you want with less headaches.
[peakheights, locations] = findpeaks(x, 'MinPeakDistance', 1000, 'MinPeakHeight', 2);
extractwindow = @(loc) [nan(1, max(0, 300-loc)), x(max(1, loc-299) : min(end, loc+700)), nan(1, max(0, 700+loc-numel(x)))];
%the anonymous function extractwindow works regardless of the location of the peak (even within the 1st 300 points or last 700 points)
%it will pad the pulse with NaN if necessary to make it 1000 points long.
pulses = cell2mat(arrayfun(extractwindow, locations(:), 'UniformOutput', false));

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Fourier Analysis and Filtering finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by