How can I find out the diagonal elements of any matrics? Again i want to arrange it in row by row

suppose i have a matrix as a=[1 2 3 4; 5 6 7 8; 9 10 11 12]; i want the result as b=[0 0 9; 0 5 10; 1 6 11; 2 7 12; 3 8 0; 0 0 4];

Answers (1)

b = spdiags(a).';
general case
b = spdiags(a,-size(a,1)+1:size(a,2)-1).';

This question is closed.

Tags

Asked:

on 20 Mar 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!