[英]diag error: nrow or ncol cannot be specified when x is a matrix
Error in diag(nrow(V) * tausq, nrow = ncol(V), ncol = ncol(V)) : 'nrow' or 'ncol' cannot be specified when 'x' is a matrix
diag(nrow(V) * tausq, nrow = ncol(V), ncol = ncol(V)) 中的错误:当“x”是矩阵时,不能指定“nrow”或“ncol”
This is the error I get when I try to run这是我尝试运行时遇到的错误
D <- diag(nrow(V)*tausq, nrow=ncol(V), ncol=ncol(V))
which is part of a function I wrote.这是我编写的函数的一部分。
It's the first line of the function and V is a matrix which is part of the argument.它是函数的第一行,V 是一个矩阵,它是参数的一部分。
What does this error mean?这个错误是什么意思?
From ?diag
, you can read that the function does one of two things -- it either extracts the main diagonal of a passed matrix or it constructs a new diagonal matrix.从
?diag
,您可以了解到该函数执行以下两件事之一——它要么提取传递矩阵的主对角线,要么构造一个新的对角矩阵。 If you are trying to extract the main diagonal of a matrix (by passing a matrix as the first argument to diag
), then you can't pass the nrow
and ncol
arguments to the diag
function.如果您正试图(通过传递矩阵作为第一个参数来提取矩阵的主对角线
diag
),那么你就无法通过nrow
和ncol
参数的diag
功能。
The error is telling you that apparently nrow(V)*tausq
is a matrix in your code -- since nrow(V)
is a constant, we conclude that tausq
must be a matrix.错误告诉您显然
nrow(V)*tausq
是您代码中的矩阵——因为nrow(V)
是一个常数,我们得出结论tausq
必须是一个矩阵。 As a result, it's giving you an error due to setting nrow
and ncol
.其结果是,它给你一个错误,由于设置
nrow
和ncol
。
Long story short -- you seem to be assuming nrow(V)*tausq
is a constant, but in fact it's a matrix.长话短说——你似乎假设
nrow(V)*tausq
是一个常数,但实际上它是一个矩阵。 You need to convert tausq
to a constant to proceed as you want to.您需要将
tausq
转换为常量才能按需要进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.