MATLAB: How to fix the code to produce ones along the reverse diagonal

flipmatrixreverse diagonal

Hi, I am having a problem with my code.
function I = reverse_diag(n)
I = zeros(n);
I(1: n+1 : n^2)=1;
I want my code to produce the ones on the reverse diagonal (top right to bottom left). I tried using fliplr because I believe, as of now, this is just a diagonal of ones from top left to bottom right. However, that is not working. Any suggestions?

Best Answer

Assuming you can’t use the eye function, this works:
n = 5;
I = zeros(n);
for k1 = 1:n
I(k1, end-k1+1) = 1;
end
I =
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
EDIT — Added output matrix.