MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Mate 2u on 26 Jul 2013

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.

*No products are associated with this question.*

Answer by dpb on 27 Jul 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

Answer by Image Analyst on 27 Jul 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]);

Show 2 older comments

dpb on 27 Jul 2013

hmmm...

>> lookfor blk blkdiag slvblk fdesignblkfcn dspblksstatusbar dspblkstructures isspblksinstalled >> lookfor block ... blkdiag saveas catch spmd_feval try cdf2rdf ldl slvblk - Solve almost block-diagonal linear system. blockGetDialogSchema - This is the default schema files for all SL blocks. vipaviread - Read AVI file. Used by Video & Image Processing Blockset's ChkIfBlockReusable - Checks whether Filter block is reusable dspblkstructures - Returns the structures supported by the Digital Filter Block. isspblksinstalled - Returns true if Simulink and Signal Processing Blockset are installed. lnkfvtool2mask - Callback for linking FVTool to a block mask. dfiltfftfirdemo - Overlap-add FIR Block filter object >>

Maybe in some toolbox, not in base Matlab???

Image Analyst on 28 Jul 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.

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/83278#comment_161730

Is this 'n' a sliding window or sequential?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/83278#comment_161731

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.