Reputation: 13
When the following code is run in R, an error message pops up:
"Error in Xt[i, ] <- F * Xt[i - 1, ] + V : number of items to replace is not a multiple of replacement length"
Appreciate your help.
X=matrix(rnorm(6),100,6)
n = nrow(X)
F = diag(1,6,6); F[1,4] = F[2,5] = F[3,6] = 1
#create process & measurement noises (standard deviation)
W = diag(rnorm(1,0,0.01),6,6)
V = diag(rnorm(1,0,0.02),6,6)
#create and init Xt and Yt matrices
Xt = matrix(NA, nrow=n+1, ncol=6)
Yt = matrix(NA, nrow=n, ncol=6)
Xt[1,] = X[1,]
#test:
F * Xt[1,] + V #this is fine!!!
#before entering the loop:
for (i in 2:3)
{
Xt[i,] = F * Xt[i-1,] + V #BUT this is not!!!
Yt[i,] = Xt[i,] + W
}
Upvotes: 1
Views: 157
Reputation: 16998
Take a look at the dimensions: You define
Xt[1,] = X[1,]
which gives you a 6x1-matrix
. Your next step is calculating
F * Xt[1,] + V
Since F
and V
are of dimension 6x6
this yields a new 6x6-matrix
. Check with
dim(as.matrix(X[t,1]))
dim(F)
dim(V)
Now Xt
itself is of dim 101x6
, therefore Xt[n,]
, which is a little bit unintuitively, gives a transposed 6x1
object. So, inside your loop you tried to assign an object
F * Xt[i-1,] + V
dim(F * Xt[i-1,] + V) # this gives 6x6
to
Xt[i,]
dim(as.matrix(Xt[i,])) # this gives 6x1
So your dimensions don't fit. I hope this answers your question.
I have one annotation:
F * Xt[1,] + V
The multiplication F*Xt[1,]
is an element-wise multiplication not the classical matrix-vector-multiplication. If you want to perform an A*b
multiplication with mxn
-matrix A
and nx1
-vector b
you have to use %*%
instead. In this case, V
has to be of dimension mx1
.
Upvotes: 1