Restore matrix from diagonals in matlab

Given the vector of the counter-diagonals of the matrix in the matrix, is there an easy way to reconstruct the matrix?

For example, given

x = [1 2 3 4 5 6 7 8 9] 

Is there any easy way to restore it to the next?

 1 2 4 3 5 7 6 8 9 

This is simplified a bit due to the fact that the dimensions of the source block are known. The reconstruction of rotation or transposition of the original matrix is ​​excellent, as rotation and transfer are easily reversed. Faster is better, this calculation should be performed on many x s.

Thanks!

+4
source share
1 answer

You can create a corresponding Hankel matrix and use it for sorting (it works only if the output is a square matrix!):

 x = [1 2 3 4 5 6 7 8 9]; %# find size of output (works only with square arrays) n=sqrt(length(x)); %# create Hankel matrix hh = hankel(1:n,n:(2*n-1)); %# sort to get order of elements (conveniently, sort doesn't disturb ties) [~,sortIdx]=sort(hh(:)); %# reshape and transpose out = reshape(x(sortIdx),n,n)'; %'# SO formatting out = 1 2 4 3 5 7 6 8 9 
+4
source

All Articles