# Sliding window in a matrix

23 views (last 30 days)
Diana on 10 Nov 2015
Commented: Joseph Cheng on 12 Nov 2015
Hello Guys, I have this code that runs through a matrix of (6000,3). My code runs each column and then each element in the column (rows). If the values of the column are bigger than the threshold it returns one and if they are smaller returns 0. But it is to sensitive for my data, I want to use a window that runs through 500 elements in the column and if half or more of them are bigger than the threshold give one to that entire window. How do I do this? So far its running each element at the time. This is the code:
for i=1:1:3; %runns trhough the matrix
SD=std(E(:,i))
thresh=SD
for ii=1:1:length(E(:,i)); %runs trhough every element
if E(ii,i) > thresh;
detect(ii,i)=1;
else
detect(ii,i)=0;
end
end
end
Thanks!

Joseph Cheng on 10 Nov 2015
you can perform what you're doing a bit simpler.
E = randi(100,6000,3); %my generation of dummy data
windowL =500;
thresh= 50;
for ind = 1:length(E(:,1))-windowL+1 % set the end such that you don't index more than the length of the array E.
Window = E(ind:ind+windowL-1); %pulled out section of E to be used
tWindow = Window>thresh; % threshold the window you'll get a 500x3 logical array
stWindow = sum(tWindow); % then take the sum to count how many are over the threshold per column (you'll end up with a 1x3 matrix of how many are over the threshold.
tstwindow = stWindow>windowL/2; % then you can threshold again to see which ones have a majority over the threshold.
detect(ind,:) = ttwindow; %then set your detected array based on the last calculation.
end
I broke up the code a bit to show some intermediate steps showing how using logical operations you can perform these things.
Joseph Cheng on 12 Nov 2015
so... i started you off. I did not know what your conditions are when the 500 sliding window started to leave the 6000 matrix.