How to get the original matrix back?

2 visualizaciones (últimos 30 días)
Ammy
Ammy el 5 de Dic. de 2021
Comentada: Ammy el 5 de Dic. de 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.

Respuesta aceptada

DGM
DGM el 5 de Dic. de 2021
Editada: DGM el 5 de Dic. de 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.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by