finding the location of the maximum of N periods

5 ビュー (過去 30 日間)
Mate 2u
Mate 2u 2013 年 7 月 26 日
Hi all, I have prices of a financial time series in a matrix "Thai" of size "4048x1".
I want to create a new matrix of size 4048x1 full of zeros and 1s , where the 1 shows the maximum point within a n period in 4048. So for instance in 4048 prices, we check the first 200 prices for the maximum, then the next 200 prices find the maximum if n = 200.....and so on.
  2 件のコメント
dpb 2013 年 7 月 26 日
Is this 'n' a sliding window or sequential?
Mate 2u
Mate 2u 2013 年 7 月 26 日
編集済み: Mate 2u 2013 年 7 月 26 日
erm....I think 1:200, 201:401, 401:601....etc
so lets say we have a 1000x1 matrix....we will have a 1000x1 matrix full of zeros with 5 1's and 995 0's, with the first 1 showing max location within 0:200, and so on.


回答 (2 件)

dpb 2013 年 7 月 27 日
OK, for "dead ahead" loop solution is fairly trivial--it didn't come to me otomh on vectorizing this owing to the indexing being inconsistent but there's bound to be a way; if the 'aha!' moment strikes I'll try to get back...
But, anyway...
function maxes=maxvec(x,N)
% For vectors, maxvec(X,N) is a vector of same length as X
% containing the maximum value of X over groups of N elements
% returned in the position found in X.
% Not yet implemented for matrices.
if size(x,1)==1,error('X must be a column vector'), end
if numel(x)~=length(x),error('X must be a column vector'), end
if mod(L,N), error('Length of X not evenly divisible by N'), end
for i=1:N:L-N+1

Image Analyst
Image Analyst 2013 年 7 月 27 日
The most "MATLAB-ish" way to do it is to use blockproc. Here's how to do it:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
close all;
% Create data of prices with 2 places to the right of the decimal.
theSignal = fix(10000 * rand(1, 4048)) / 100;
% Now let's use an anonymous function.
% We'll take the max in the blocks.
windowSize = 200;
myFilterHandle = @(block_struct) ...
max( * ones(size(;
% Find the max in each 200 element long block.
blockyMax = blockproc(theSignal, [1, windowSize], myFilterHandle);
[rowsM, columnsM, numberOfColorChannelsSD] = size(blockyMax);
% Plot it.
subplot(2, 1, 1);
plot(blockyMax, 'bo-');
caption = sprintf('Signal Processed in 1 by %d Blocks\n%d by %d pixels\nAnonymous Max Filter', ...
windowSize, rowsM, columnsM);
title(caption, 'FontSize', fontSize);
% Find where the signal equals the block-wise max.
signalEqualMax = (theSignal == blockyMax);
% Plot it.
subplot(2, 1, 2);
plot(signalEqualMax, 'bo-');
title('1 where signal equals the max in a 200 long window', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
  5 件のコメント
Image Analyst
Image Analyst 2013 年 7 月 28 日
Oh, it's in the Image Processing Toolbox - I didn't realize that until now because so many people use it for non-imaging applications. In R2010b then changed the name from blkproc to blockproc.
dpb 2013 年 7 月 28 日
Ahhhh....not one included in what TMW kindly provided...



Community Treasure Hunt

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

Start Hunting!

Translated by