How do I fix my code to produce ones along the reverse 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?

2 Comments

Alexandra, you might like to read this link on formatting and this link so you can post better questions. You put code as text, and text as code format. I'll fix it this time for you. Also, you might give more descriptive subject lines - all your posts are like "how do I fix my code?" even though they're on different topic.
Don't forget to look at my answer below.
function s = reverse_diag(n)
I = zeros(n);
I(1: n+1 : n^2)=1;
s = flip(I, 2) % this line will reverse the elements in each row.

Sign in to comment.

 Accepted 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.

More Answers (3)

Yet another way using linear indexing:
I = zeros(n);
I(n:n-1:end-1) = 1;

6 Comments

A=[1 2 3 4 5;7 5 4 8 4;7 34 5 6 3;1 2 3 4 57; 34 5 6 3 0]
A =
1 2 3 4 5
7 5 4 8 4
7 34 5 6 3
1 2 3 4 57
34 5 6 3 0
>> A(5)
ans =
34
>> A(6)
ans =
2
>> A(1:5)
ans =
1 7 7 1 34
>> A(1:8)
ans =
1 7 7 1 34 2 5 34
get it every element of the matrix has a location number the location number of the reverse diagonal elements is (n:n-1:end-1).try n=5 with the above matrix
but why are there 3 location numbers for the reverse diagonal - n, n-1, end-1? In your example, you only show 2 location numbers - 1:5 or 1:8. Please look at my example below. Why doesnt the code work with n=5?
I =
7 7 10 3 8
9 8 10 4 8
7 1 5 4 6
7 1 4 6 4
10 3 10 6 2
>> I(5)
ans =
10
>> I(1:5)
ans =
7 9 7 7 10
>> I(2:10)
ans =
9 7 7 10 7 8 1 1 3
>> I(5:4:4)
ans =
1×0 empty double row vector
The syntax
I(n:n-1:end-1)
does not index just three locations, but the linear index n from n^2-1 ("end" is equal to n^2) with the step (n-1), which gives the entire anti-diagonal.
The antidiagonal are indexed by row-colum of (r(i),c(i)), i=1,...n, where:
r = n:-1:1
c = 1:n;
If you take a linear index of those
sub2ind([n n], r, c)
You will get exactly
n:n-1:n^2-1
Example with n=5:
>> n=5;
>> r=n:-1:1
r =
5 4 3 2 1
>> c=1:n
c =
1 2 3 4 5
>> i = sub2ind([n n], r, c)
i =
5 9 13 17 21
>> n:n-1:n^2-1 % == (n:n-1:end-1)
ans =
5 9 13 17 21
>>
I tried this piece of code:
I = zeros(n);
I(n:n-1:end-1) = 1;
The result when n=1 provides answer [0] instead of [1]. All the other size matrices works fine.
Correct, this is a bug for n==1. One can make it works for any n>=1 (but still not for n==0) with
I([1,n:n-1:1-1]) = 1;

Sign in to comment.

Try this:
n = 5; % Whatever...
I = fliplr(eye(n))
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
Hi,
I'm a little late but I'm taking this class on coursera and here's my answer:
function I = reverse_diag(n)
I = zeros(n);
I(end-(n-1):-(n-1) : n)=1;
end
Hope it'll help someone

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!