How do I fix my code to produce ones along the reverse diagonal?
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 7 Aug. 2016
Bearbeitet: Image Analyst
am 7 Aug. 2016
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.
Nava Subedi
am 15 Nov. 2016
Bearbeitet: Nava Subedi
am 15 Nov. 2016
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.
Akzeptierte Antwort
Weitere Antworten (3)
James Tursa
am 15 Nov. 2016
Yet another way using linear indexing:
I = zeros(n);
I(n:n-1:end-1) = 1;
6 Kommentare
Swapnil Deokar
am 7 Mär. 2018
Can you explain this.
Ahmed Diaa
am 14 Apr. 2018
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
Kazi Anisha Islam
am 14 Okt. 2018
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
Bruno Luong
am 14 Okt. 2018
Bearbeitet: Bruno Luong
am 14 Okt. 2018
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
>>
Harshith Dhananjaya
am 9 Jun. 2020
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.
Bruno Luong
am 12 Jun. 2020
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;
Image Analyst
am 7 Aug. 2016
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
mouellou
am 21 Dez. 2018
0 Stimmen
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
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!