Warm tip: This article is reproduced from serverfault.com, please click

How to unfold a Matrix on Matlab?

发布于 2020-11-28 12:58:38

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 : enter image description here

enter image description here

enter image description here

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

enter image description here

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

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

enter image description here

Any suggestions?

Questioner
m2016b
Viewed
0
Luis Mendo 2020-11-29 00:08:49

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{:});