finding the location of the maximum of N periods
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
dpb
el 27 de Jul. de 2013
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
0 comentarios
Image Analyst
el 27 de Jul. de 2013
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 comentarios
Image Analyst
el 28 de Jul. de 2013
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.
Ver también
Categorías
Más información sobre Single-Rate Filters en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!