Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

tmax<-10
t<-1:tmax
n<-matrix(0, nr=5, ncol=tmax+1)
n
n[,1]<-c(10, 10, 10, 10, 10)

for(i in (1:tmax)) {n[,i+1]<-A%*%n[,i]}
N<-numeric(tmax)
N

for(i in (1:tmax)) {N[i]<-sum(n[,i])}

plot(t-1, N, type='l', xlab="t", ylab="N")

plot(t-1, n[1,], type='l', xlab="t", ylab="N")
Error in xy.coords(x, y, xlabel, ylabel, log) :    'x' and 'y' lengths differ

How do i fix this error?

t-1 has tmax rows but n[1,] has tmax+1 rows, so in this case 10 and 11 rows respectively, that is why you are getting that error message, if you match both lengths the plot works.

tmax <- 10
t <- 1:tmax
n <- matrix(0, nr = 5, ncol = tmax + 1)
n[,1] <- c(10, 10, 10, 10, 10)

plot(t-1, head(n[1,], tmax), type='l', xlab="t", ylab="N")

Created on 2019-11-20 by the reprex package (v0.3.0.9000)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.