Skip to content

Instantly share code, notes, and snippets.

@werediver
Created March 12, 2014 13:04
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/9506496 to your computer and use it in GitHub Desktop.
Save werediver/9506496 to your computer and use it in GitHub Desktop.
//-->fill(3,5)
// ans =
// 1. 3. 4. 9. 10.
// 2. 5. 8. 11. 14.
// 6. 7. 12. 13. 15.
function [x] = fill(N, M)
x = zeros(N, M)
Z = N + M - 1
i = 1
for z = 1 : Z
if modulo(z, 2) == 1 then
for row = 1 : min(z, N)
col = z - row + 1
if col <= M then
x(row, col) = i
i = i + 1
end
end
else
for col = 1 : min(z, M)
row = z - col + 1
if row <= N then
x(row, col) = i
i = i + 1
end
end
end
end
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment