finding the location of the maximum of N periods

2 次查看(过去 30 天)
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 个评论
Mate 2u
Mate 2u 2013-7-26
编辑:Mate 2u 2013-7-26
erm....I think sequential....so 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
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
L=length(x);
if mod(L,N), error('Length of X not evenly divisible by N'), end
maxes=zeros(size(x));
for i=1:N:L-N+1
[xm,ix]=max(x(i:i+N-1));
maxes(i+ix-1)=xm;
end

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(block_struct.data) * ones(size(block_struct.data));
% 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
dpb 2013-7-28
Ahhhh....not one included in what TMW kindly provided...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Single-Rate Filters 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by