MATLAB: How to find n values before and after a maximum of an array

data around maxseparate a bell curve from data

Hi, everybody, new to Matlab. I want to be able to locate the maximun peak which is known(always at 1) and to be able to extract the data right before and after this maximun peak(see example on figure). And this should be done even if the max-peak value changes along the x-as. Please help 🙁

Best Answer

  • Something like this will work
    x; % array of x-values
    y; % array of y-values
    [max_y, max_idx] = max(y);
    x_max = x(max_idx);
    interval = 0.02; % length of interval before and after peak
    idx = (x_max-interval < x) & (x < x_max+interval);
    x_interval = x(idx);
    y_interval = y(idx);
    x_interval and y_interval contain values in around the peak.