# What can be a reverse permutation formula of n*m matrix

2 views (last 30 days)
dani elias on 7 Sep 2020
Commented: dani elias on 8 Sep 2020
The first row remain as it is, but from the second to the end it changed as shown below: Given A as original matrix, B the result matrix. the first row at B remain as it was in A, but at second row, the first element is obtained After add the first element at row above and it's last element in corresponding row, such as B[2,1]=A[1,1]+A[2,4] which is 9=5+4. But the second element at row two is B[2,2]=A[2,1]+A[1,2] which is 13=10+3. So 13=11+2,4=3+1, again for next row B[3,1]=8+10, B[3,2]= 6+11 etc..
A=[4 3 2 1;10 11 3 5; 6 4 7 8; 9 10 40 5]
B=[ 4 3 2 1; 9 13 13 4; 18 16 7 12; 11 23 17 48].
I need the reverse formula to its original matrix.. Assume Matrix A is of size 256*256 or 512*512
Matt J on 8 Sep 2020
I think you have incorrect entries in B(3:4,2). I think the real B matrix should be,
B =
4 3 2 1
9 13 13 4
18 17 7 12
11 13 17 48

Matt J on 8 Sep 2020
Edited: Matt J on 8 Sep 2020
Assuming A and B are always square,
n=length(A);
E=speye(n);
S=circshift(E,[1,0]);
T=kron(S,E)+kron(E,S);
T(1:n,1:n)=E;
T(1:n,n+1:end)=0;
fun=@(Z) reshape(Z.',[],1);
ifun=@(Z) reshape(Z,n,n).';
A=ifun(T\fun(B))
dani elias on 8 Sep 2020
Thank you. I do appreciate.

Matt J on 8 Sep 2020
This version is also quite fast - maybe even faster than my other, fully vectorized answer.
n=length(A);
%% Forward
tic;
B=A;
B(2:end,:)=A(1:end-1,:)+circshift(A(2:end,:),[0,1]);
toc
%% Inverse
A=B;
tic
for i=2:n
A(i,:)=circshift(A(i,:)-A(i-1,:),[0,-1]);
end
toc
dani elias on 8 Sep 2020
Thank you. I do appreciate your contributions.

### Categories

Find more on Creating and Concatenating 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!

Translated by