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

1 次查看(过去 30 天)
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
  1 个评论
Matt J
Matt J 2020-9-8
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
Matt J 2020-9-8
编辑:Matt J 2020-9-8
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))
  5 个评论

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2020-9-8
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by