% Permutations of 1 2 3 4 5 in lexicographical order: A = sortrows( perms(1:5) ); % In reverse lexicographical order: B = rot90(A,2); whos Name Size Bytes Class Attributes A 120x5 4800 double B 120x5 4800 double % Add the integers from 6 to 120 to each row, % which are not changed in their position by the permutations: for k=1:120 C(k,1:115)=[6:120]; end whos C Name Size Bytes Class Attributes C 120x115 110400 double D = cell2mat( { B C } ); whos D Name Size Bytes Class Attributes D 120x120 115200 double % Reflect the square matrix, to calculate the diagonals: E = rot90(D)'; % Calculate the diagonals: for k=1:120 X(((k^2-k)/2+1):((k^2+k)/2)) = diag(E,120-k); end whos X Name Size Bytes Class Attributes X 1x7260 58080 double % Show first 55 enries of the sequence X: X(1:55) ans = Columns 1 through 10 1 2 2 3 1 1 4 3 3 3 Columns 11 through 20 5 4 2 1 2 6 5 4 2 3 Columns 21 through 30 3 7 6 5 4 1 2 1 8 7 Columns 31 through 40 6 5 4 1 2 2 9 8 7 6 Columns 41 through 50 5 4 4 1 1 10 9 8 7 6 Columns 51 through 55 5 3 4 4 4 % These are the first 10 antidiagonals of the square matrix D: X(1) ans = 1 X(2:3) ans = 2 2 X(4:6) ans = 3 1 1 X(7:10) ans = 4 3 3 3 X(11:15) ans = 5 4 2 1 2 X(16:21) ans = 6 5 4 2 3 3 X(22:28) ans = 7 6 5 4 1 2 1 X(29:36) ans = 8 7 6 5 4 1 2 2 X(37:45) ans = 9 8 7 6 5 4 4 1 1 X(46:55) ans = 10 9 8 7 6 5 3 4 4 4