how to expand a Square matrix and reverse to its original form ?

4 ビュー (過去 30 日間)
dani elias
dani elias 2022 年 8 月 28 日
コメント済み: dani elias 2022 年 8 月 31 日
if you want to expand a 2x2 to 4x4, or 8x8 to 16x16 or any square matrix, how can you do it using bitxor operation and reverse it to original matrix form? the concept operate like Hadamard code ..example a 2x2 original matrix has been converted to 4x4 matrix called new matrix as shown below
original=[1 2;3 4];
b=bitxor(1,[0 2;3 4]);
c=bitxor(2,[1 0;3 4]);
d=bitxor(3,[1 2;0 4]);
e=bitxor(4,[1 2;3 0]);
newmatrix = [b c;d e]
newmatrix = 4×4
1 3 3 2 2 5 1 6 2 1 5 6 3 7 7 4
The values at position (1,1)=1,(1,4)=2,(4,1)=3 and (4,4)=4 which are equal to the original matrix. and during the operation in each multiplier position we replace it by zero so that it remain with the same value as original after operation

採用された回答

Voss
Voss 2022 年 8 月 28 日
編集済み: Voss 2022 年 8 月 30 日
Here's some code that generalizes the example for any n (size of original matrix), except this expands the matrix to size n^2-by-n^2. (It's not clear (to me) how the process would work for n > 2 in order to generate a matrix of size 2n-by-2n.)
original = [1 2; 3 4];
n = size(original,1);
newmatrix = zeros(n^2);
for ii = 1:n
rows = n*(ii-1)+(1:n);
for jj = 1:n
cols = n*(jj-1)+(1:n);
temp = original;
temp(ii,jj) = 0;
newmatrix(rows,cols) = bitxor(original(ii,jj),temp);
end
end
newmatrix
newmatrix = 4×4
1 3 3 2 2 5 1 6 2 1 5 6 3 7 7 4
And to get the original matrix back:
idx = 1+(n+1)*(0:n-1);
neworiginal = newmatrix(idx,idx)
neworiginal = 2×2
1 2 3 4
  5 件のコメント
Bruno Luong
Bruno Luong 2022 年 8 月 30 日
newmatrix = zeros(2*n)
I pretend your allocation is ineffective; the size is n^2.
Voss
Voss 2022 年 8 月 30 日
@Bruno Luong Thank you. Corrected.

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 8 月 30 日
Not fully check:
original=[1 2;3 4]
original = 2×2
1 2 3 4
n = size(original,1);
A = reshape(original,[n 1 n 1]);
A = repmat(A,[1 n 1 n]);
[I,J] = ndgrid(1:n,1:n);
A(sub2ind(n+zeros(1,4),I,I,J,J)) = 0;
B = reshape(original,[1 n 1 n]);
C = reshape(bitxor(A,B),n^2+zeros(1,2))
C = 4×4
1 3 3 2 2 5 1 6 2 1 5 6 3 7 7 4
  2 件のコメント
Bruno Luong
Bruno Luong 2022 年 8 月 30 日
Recover the original
% reverse
n2 = size(C,1);
n = sqrt(n2);
[I,J] = ndgrid(1:n,1:n);
K = sub2ind(n+zeros(1,4),I,I,J,J);
original = reshape(C(K),[n n])
dani elias
dani elias 2022 年 8 月 31 日
I do appreciate. Thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBit-Wise Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by