MATLAB: Matrix manupulation and reading diagonally

matrix manipulation

Hello Team,
i have a matrix
Matrix_test = [13,10,5,2;7,14,11,6;3,8,15,12;1,4,9,16];
I want to read the matrix in the order mentioned in the matrix.
Starting from left bottom (1), then right top (2) and then again 2nd diagonal from left (3,4) and then 2nd right diagonal (5,6) and so on and so forth.
Please support in resolving it.
Edit : i there any way to create this kind of matrix for any n-by-n matrix automatically. i have created this Matrix_test manually but if i want 8×8 matrix in similar pattern . How to create it through generic code.
Thanks in advance.

Best Answer

This function may help:
M = [13,10,5,2;7,14,11,6;3,8,15,12;1,4,9,16]
MyResult = readDiag(M)
function R = readDiag(M)
R = cell(2,numel(diag(M)));
for k = 1:numel(diag(M))-1
R{1,k} = diag(M,-numel(diag(M))+k);
R{2,k} = diag(M,numel(diag(M))-k);
end
R = reshape(R,[],1);
R = [R; {diag(M)}];
R = cell2mat(R);
end
result is:
M =
13 10 5 2
7 14 11 6
3 8 15 12
1 4 9 16
MyResult =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16