3

I want to plot all the diagonals of a matrix. In the matrix row 1 contains information of time 1, row 2 of time 2 etc etc. Each diagonal presents the evolution of the number of fishes in a cohort that appeared at a given point in time.

Does there exist some inbuilt function in matlab for this problem? This is the smartest code I could think of. But I mis lines.

close 
clear
clc
%A=[10 20 30 45 55;11 22 30 44 51; 10 20 30 40 50; 10 20 35 40 48];
A=[1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5]
r=size(A,1);
c=size(A,2);
p=c-r+1;%number of diagonals
T=size(diag(A),1);%T=min(S,N)
x=linspace(1,T,T);
plot(x,diag(A))
hold on
%plot the subdiagonals
for i=r:-1:1
 plot(r-i+1:r, diag(A, i-r));
end 
%plot the diagonals
for j=1:p
 plot(1:r,diag(A,j-1));
end
%plot the superdiagonals
for k=1:(c-p)
 plot (1:k, diag(A, c-k));
end
asked Nov 5 at 6:02
7
  • If I change the matrix I got errors. That is the problem of this code. I have also another code that always works. But only for a 4 x 5 matrix. I would like to have a code that work for any matrix. This is the other code I have A=[10 20 30 45 55;11 22 30 44 51; 10 20 30 40 50; 10 20 35 40 48]; S=size(A,1); N=size(A,2); T=size(diag(A),1);%T=min(S,N) x=linspace(1,T,T); y=diag(A); plot (x,y) hold on plot(4:4,diag(A,-3)) plot(3:4,diag(A,-2)) plot(2:4,diag(A,-1)) plot(1:4,diag(A,0)) plot(1:4,diag(A,1)) plot(1:3,diag(A,2)) plot(1:2,diag(A,3)) plot(1:1,diag(A,4)) Commented Nov 5 at 17:20
  • 1
    It's not completely clear to me what the x coordinates of these diagonal plots should be. In the code snippet you shared, the diagonals and superdiagonals start at x = 1, while the subdiagonals are shifted to the right such that they end at the right margin of the plot. With that consideration, it appears that your code snippet works fine in all cases when r <= s, including for the second example from your comment. If that's not the case, please explain what you expected to get. There are corrections for the case r > s, the simplest one being to transpose the matrix. Commented Nov 6 at 5:42
  • Yes exactly, the evaluation of the numbers of a cohort is followed along diagonals. In row 1, you have 5 groups (diagonals) each one starts at x=1 (time 0). The first subdiagonal represents a group starting at x=2 (time 1) and the dynamic can be followed along the diagonal: A(2,1)-A(3,2)-A(4,3). The second subdiagonal represents a group starting at x=3 (time 2) and the evolution of the y can be followed along the diagonal: A(3,1)-A(4,2). The problem is how to plot this smartly for bigger matrices. Commented Nov 6 at 11:56
  • I think the safest bet is to plot the data as it is, since your users are probably expecting that. Commented Nov 6 at 15:30
  • In this github repository I added uploaded three versions of the code in the question. You may check them and let me know if there are cases they don't work as expected and let me know what your expected result was. Commented Nov 6 at 15:31

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.