How to unfold a Matrix on Matlab?

≡放荡痞女 提交于 2021-01-28 11:31:15

问题


I have a given matrix H and I would like to unfold (expand) it to find a matrix B by following the method below :

Let H be a matrix of dimension m × n. Let x = gcd (m,n)

  1. The matrix H is cut in two parts.
  2. The cutting pattern being such that :
  • The "diagonal cut" is made by alternately moving c = n/x units to the right (we move c units to the right several times).
  • We alternately move c-b = m/x units down (i.e. b = (n-m)/x) (we move b units down several times).
  1. After applying this "diagonal cut" of the matrix, we copy and paste the two parts repeatedly to obtain the matrix B.

Exemple : Let the matrix H of dimension m × n = 5 × 10 defined by :

 1 0 1 1 1 0 1 1 0 0  
 0 1 1 0 0 1 1 0 1 1  
 1 1 0 1 1 1 0 1 0 0  
 0 1 1 0 1 0 1 0 1 1  
 1 0 0 1 0 1 0 1 1 1
  • Let's calculate x = gcd (m,n) = gcd (5,10) = 5,
  • Alternatively move to the right : c = n/x = 10/5 = 2,
  • Alternatively move down : b = (n-m)/x = (10-5)/5 = 1.
  1. Diagonal cutting diagram : The matrix H is cut in two parts. The cutting pattern is such that :
  • We move c = 2 units to the right several times c = 2 units to the right,
  • We repeatedly move c - b = 1 unit downwards.

We get :

  1. After applying this "diagonal cut" of the matrix, we copy and paste the two parts repeatedly to obtain the matrix :

Remark : In the matrices X, X1 and X2 the dashes are zeros.

  1. The resulting matrix B is (L is factor) :

Any suggestions?


回答1:


This can be done by creating a logical mask with the cutting pattern, and then element-wise multiplying the input by the mask and by its negation. Repeating by L can be done with blkdiag.

H = [1 0 1 1 1 0 1 1 0 0
     0 1 1 0 0 1 1 0 1 1
     1 1 0 1 1 1 0 1 0 0
     0 1 1 0 1 0 1 0 1 1
     1 0 0 1 0 1 0 1 1 1];
L = 2;
[m, n] = size(H);
x = gcd(m, n);
c = n / x;
b = (n-m)/x;
mask = repelem(tril(true(m/b)), b, c);
A = [H.*mask; H.*~mask];
A = repmat({A}, L, 1);
B = blkdiag(A{:});


来源:https://stackoverflow.com/questions/65049810/how-to-unfold-a-matrix-on-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!