Skip to content

Instantly share code, notes, and snippets.

@werediver
Created March 12, 2014 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werediver/9506561 to your computer and use it in GitHub Desktop.
Save werediver/9506561 to your computer and use it in GitHub Desktop.
Scilab function returning 2D matrix antidiagonal by index.
//-->adiag([1 2 3 4; 5 6 7 8; 9 10 11 12],3)'
// ans =
// 9. 6. 3.
function [v] = adiag(x, z)
// 2D matrix antidiagonal
[N, M] = size(x)
// The total diagonals count
Z = N + M - 1
// The current diagonal length
z_len = min(z, Z - z + 1, min(N, M))
i = max(z, N * (z - N + 1))
step = N - 1
v = x(i : step : i + step * z_len - 1)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment