How to get the original matrix back?

2 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by