I need some information on how to program the sliding window of matrix.
I have a diagonal matrix B defined below by:
I would like to deduce from the given matrix B and the size of the window W the different matrix for t ranging from 1 to the size L of the matrix.
Note: Each window has the same width and height. And the window moves on the diagonal.
Example: We have a matrix with size 5 by 8 and the size of the window is 3 by 4 and moves on the diagonal. The matrix B of the example is:
My code:
% Sliding window matrix
B = [ 1 1 0 0 0 0 0 0
2 2 1 1 0 0 0 0
0 0 2 2 1 1 0 0
0 0 0 0 2 2 1 1
0 0 0 0 0 0 2 2]; % Matrix B
W = 4; % Size of the window wanted here is 4
n = size(B,2) - W + 1;
X = zeros(size(B,1),W*n);
k = W-1:-1:0;
for i = 1:n
window = B(:,i:i+W-1)';
X(:,i*W - k) = B(:,i:i+W-1);
end
window;
With W = 4 and n of the for loop set to 1 I get:
window = [1 2 0 0 0
1 2 0 0 0
0 1 2 0 0
0 1 2 0 0];
Whereas I should get:
window = [1 1 0 0
2 2 1 1
0 0 2 2];
With my code I don't get exactly the different sub-matrix obtained by sliding window.
I would like my program to be able, depending on the matrix B and the choice of dimensions of my window, to return the diagonal matrix representing the window and shift one step to also recover the next diagonal matrix which is identical to the previous one, and so on until the end.
Any suggestions?




Bis unknown. Also specify the desired outcome. - saastnwindow = B(:,i:i+W-1)';in each loop iteration? You don't use it in the loop. You could also assignwindow = B(:,n:n+W-1)';after the loop with the same result. What do you expect fromwindow;? In the image the window has a width and a height and is moved on the diagonal. In the code the window has the full height of B and is moved in horizontal direction. - Thomas Sablik