MATLAB: Triangle matrix with different number of columns

matrix arraymatrix manipulation

how to make a triangle matrix, from a matrix example like this
a =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
and the matrix that i'm looking for will like this a =
35 1 6 26 19 24
0 32 7 21 23 0
0 0 2 22 0 0
Thanks, Rio

Best Answer

Use triu (or tril to get triangular matrices. Use flipr on the matrix and on the output, if you want to apply them antidiagonally (which is an odd thing to want):
a = [ 35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11];
out = fliplr(triu(fliplr(triu(a(1:3, :)))))