finding the location of the maximum of N periods

조회 수: 2 (최근 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
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 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