Dear all
I have a matrix with 27 columns. The rows are time periods. Each column takes value 1, 0 or -1. For instance, a typical column would look like this:
%
001000-10010000-1001end
If you imagine a time series, values of 1 mean a peak, values of -1 mean a trough. Hence, we can define a period of expansion as the period between any t=1 and t=-1 (including these two endpoints).
I want to create a matrix such that each column has 1's in every period belonging to an expansion period and 0's otherwise. For instance, the column above would become
%
001111100111111000end
My question is how to this in an efficient manner and in a way that applies to every column of the matrix at once. I struggling with the end values. For instance, the last value is a 1 but since there is no -1 after it, it should become a 0!
Thanks for your help
James
Edit: Btw, this is what I have so far…. it has been running for quite a while so I have no idea whether it works or not.. but even if it does it really is inefficient
% [R, C] = size(mBBpre); %mBBpre is the matrix of 0's,1's and -1's
mExpansionPre = NaN(R,C); %this is where I want to put the values
for j = 1 : C % iLocalPeak is the number of Peaks for column j
iLocalPeak = vPeaksPre(j,1); % iLocalTrough is the number of Troughs for column j
iLocalTrough = vTroughPre(j,1); % Pre-allocates space
vPositionPeaksPre = NaN(iLocalPeak,1); % Pre-allocates space
vPositionTroughPre = NaN(iLocalTrough,1); % Stores Positions of Peaks
vPositionPeaksPre = find(mBBpre(:,j) == 1); % Stores Positions of Troughs
vPositionTroughPre = find(mBBpre(:,j) == -1); for k = 1 : numel(vPositionPeaksPre) for i = 1 : numel(mExpansionPre) if i >= vPositionPeaksPre(k,1) && i <= vPositionTroughPre(k,1) mExpansionPre(i,j) = 1; else mExpansionPre(i,j) = 0; end end end end end
Best Answer