I'm trying to figure out how to create a new matrix with all the diagonals arranged column wise.For example say I have the following matrix
0 1 2 7 0 0 0 0
0 0 3 6 7 0 0 0
0 0 0 3 1 7 0 0
0 0 0 0 4 4 7 0
0 0 0 0 0 5 8 7
0 0 0 0 0 0 1 8
0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0
Extracting off diagonals we get,
1 3 3 4 5 1 4
2 6 1 4 8 8
7 7 7 7 7    
Now,I am searching for efficient solution in R to arrange these diagonal vectors such that the resulting matrix is
1 2 7  
3 6 7 
3 1 7 
4 4 7 
5 8 7 
1 8 0
4 0 0
0 0 0
Also,to achieve the reverse form i.e. smallest diagonal first like this
0 0 0
0 0 1
0 2 3
7 6 3
7 1 4
7 4 5
7 8 1
7 8 4
I have tried using for loop,but that solution is not computationally efficient,since the matrix can be large(10^3) I feel the efficient solution will be ridiculously simple, but I am unable to figure it out.
 
     
    