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
lang-matlab
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 caser > s, the simplest one being to transpose the matrix.