0
votes

I need some information on how to program the sliding window of matrix.

I have a diagonal matrix B defined below by:

enter image description here

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:

enter image description here

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?

1
Are you looking for a tutor to teach you how to program that, a freelancer to program that for you or are you seriously expecting that someone reads a paper with 18 pages and explains you how to implement this algorithm? "Any suggestion?" Read the paper, try to implement it and ask a specific programming question. - Thomas Sablik
Please edit your code and give a minimal reproducible example. Your current code does not run since B is unknown. Also specify the desired outcome. - saastn
Now you have a code that produces a wrong result. Matlab gives you a very strong tool for this, the debugger. Step line by line through your code until you find the exact line that behaves differently than expected. Therefore it's usually necessary to solve the problem with pen and paper before to compare the correct solution with the program. - Thomas Sablik
Why do you assign window = B(:,i:i+W-1)'; in each loop iteration? You don't use it in the loop. You could also assign window = B(:,n:n+W-1)'; after the loop with the same result. What do you expect from window;? 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
"Exactly I would like to set my code so that the window has a width and a height and moves on the diagonal. " Why you don't do it? You are setting the width and moving the window in one direction so obviously you know how to do it. - Thomas Sablik

1 Answers

1
votes

You need to specify window dimensions separately. Try this:

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];

[bh, bw] = size(B); % matrix size
wh = 3; % window height
ww = 4; % window width
sx = 2; % sliding step length along 2nd dim
sy = 1; % sliding step length along 1st dim
wx = 1:sx:(bw-ww+1); % window left
wy = 1:sy:(bh-wh+1); % window top
n = min(numel(wx), numel(wy)); % number of windows which fit in matrix

for ii = 1:n
    X = B((1:wh)+wy(ii)-1, (1:ww)+wx(ii)-1)
end

enter image description here

Note that the sliding window, as it is described in your question, does not necessarily visit the entire matrix diagonal. Unless you calculate the sx and sy based on the dimensions of the matrix.

B = randi(3, [8 13])
[bh, bw] = size(B); % matrix size
wh = 3; % window height
ww = 4; % window width
sx = 3; % slideing step length along 2nd dim
sy = 1; % slideing step length along 1st dim

enter image description here