# How to get the original matrix back?

2 views (last 30 days)
Ammy on 5 Dec 2021
Commented: Ammy on 5 Dec 2021
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 on 5 Dec 2021
Edited: DGM on 5 Dec 2021
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.
Ammy on 5 Dec 2021
Thank you very much.

### Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!