Effacer les filtres
Effacer les filtres

How to get the original matrix back?

2 vues (au cours des 30 derniers jours)
Ammy
Ammy le 5 Déc 2021
Commenté : Ammy le 5 Déc 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.

Réponse acceptée

DGM
DGM le 5 Déc 2021
Modifié(e) : DGM le 5 Déc 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.
  1 commentaire
Ammy
Ammy le 5 Déc 2021
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by