问题
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)
- The matrix
H
is cut in two parts. - The cutting pattern being such that :
- The "diagonal cut" is made by alternately moving
c = n/x
units to the right (we movec
units to the right several times). - We alternately move
c-b = m/x
units down (i.e.b = (n-m)/x
) (we moveb
units down several times).
- 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
.
- 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 timesc = 2
units to the right, - We repeatedly move
c - b = 1
unit downwards.
We get :
- 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.
- 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