MATLAB: Function to find max values for intervals

arrayfunctionintervalsmatrix manipulationoutputvector

I have a single column with many values . I need to find the maximum value for the first 152 numbers. I need matlab to also output the max for the next 151, then the next 151 as well as the next 151. Then I need the max for the next 152 and then the next 151 and this same pattern repeats for all the data. The alternating 152 is what makes this task seemingly impossible .

Best Answer

Try this:
x = sf; % Set ‘x’ = ‘sf’
collen = 605; % Column Length (Incorporating All Sub-Sections)
xtra = rem(numel(x),collen); % Elements Beyond 605*N Segments
lenpad = ones(collen-xtra,1)*(-realmax); % Pad With Largest Negative Values (Will Not Be ‘max’ Of Other Elements)
xc = [x; lenpad]; % Append To Existing ‘x’ To Create ‘xc’
xr = reshape(xc, collen, []);
parts = [152 [1 1 1]*151];
csparts = cumsum(parts);
idxvct = [0 csparts];
for k = 1:numel(idxvct)-1
IdxCheck = [idxvct(k)+1 idxvct(k+1)] % Index Check (Delete)
idxrng = idxvct(k)+1:idxvct(k+1);
maxmtx{k,:} = max(xr(idxrng,:),[],1);
end
Out = reshape(cell2mat(maxmtx), [],1);
Out = Out(Out > -realmax);
The loop is necessary because of the differing lengths. The code creates a matrix from the initial vector, then operates on it column-wise to get the means, then uses the cell2mat and reshsape functions to create the column output corresponding to the original column vector.
The ‘IdxCheck’ vector simply reports the index range for that iteration of the loop. It is not necessary for the code. I posted it so you can be certain it partitions the vector as you want it to.