Thanks, but use of diag is explicit;y forbidden.
I need to produce a matrix that has one's along the diagonal from the upper right to the lower left
77 views (last 30 days)
I need to produce a matrix along a square matrix of size "n" that has one's along the diagonal from the upper right to the lower left. I can't use "for" or an "if" statement. Any idea?
What doesn't work is:
function Y = reverse_diag(n)
zeroes = zero(n);
x = zero;
Y = x;
More Answers (2)
Ahmet Cecen on 12 Nov 2016
You cannot index points inside a matrix like that. MATLAB indexing reads like this:
Matrix(a,b) => For every a corresponding to every b.
So what you are doing is, say for the first row, fill every column (n:-1:1), then for the second row and so forth.
What you need to do is either use a for loop manually, or use the function:
followed by the function: