How to get the original matrix back?

2 ビュー (過去 30 日間)
Ammy
Ammy 2021 年 12 月 5 日
コメント済み: Ammy 2021 年 12 月 5 日
clc;clear all;close all
A=reshape (1:16 ,4,4); % 4x4 matrix
B1 = A(1:2:end, 1:2:end);
B2 = A(1:2:end, 2:2:end);
B3= A(2:2:end, 1:2:end);
B4 = A(2:2:end, 2:2:end);
c = reshape([B1 B2 B3 B4].', 4,4)';
c(:,[2,3]) = c(:,[3,2]) % swap columns
For the above the code works properly, but the problem is that I am unable to get orignal matrix back when I use
A=reshape (1:36 ,6,6); % 6x6 matrix
In other words how can I get back 'c' for large square matrix. Is there any generalized way that works for any square matrix.

採用された回答

DGM
DGM 2021 年 12 月 5 日
編集済み: DGM 2021 年 12 月 5 日
This should work at least for even sized arrays. I'm sure there are other ways.
s = 8; % must be even
A = reshape (1:s^2 ,s,s);
B1 = A(1:2:end, 1:2:end);
B2 = A(1:2:end, 2:2:end);
B3 = A(2:2:end, 1:2:end);
B4 = A(2:2:end, 2:2:end);
c = reshape([B1 B2 B3 B4].',size(A)).'
c = permute(reshape(c,size(A,1),[],2),[1 3 2])
c = reshape(c,size(A))
When trying to "demosaic" an array with reshape(), it's helpful to split it into an extra dimension and permute.
  1 件のコメント
Ammy
Ammy 2021 年 12 月 5 日
Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by