矩阵表达式导致错误“需要数字/复杂矩阵/向量参数"? [英] Matrix expression causes error "requires numeric/complex matrix/vector arguments"?

查看:33
本文介绍了矩阵表达式导致错误“需要数字/复杂矩阵/向量参数"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ma=diag(3)+t(da)%*%da

上面的R代码,报错信息如下:

R Code above, error message as follows:

Error in t(da) %*% da : requires numeric/complex matrix/vector arguments

da 是一个矩阵,如下所示:

da is a matrix, looks as following:

V45       V46          V47          V48         V49         V50          V51    
1    0.461727059  2.357732985 -1.536932071 -1.34425710  0.893541975 -0.0676913075 -0.86532231
2    0.253022555  1.524473647 -0.588911138 -1.65207275 -0.072255170 -0.5212951533 -1.43686625
3    0.824678362  1.497001189  0.335973892 -0.84027799  0.275289411 -0.2921928001 -0.16277595
4    0.854530787  2.258305198  0.107346531 -1.69194014 -0.841572928 -1.1153931009 -1.939461341
5    1.148286984 -0.232390389 -0.498465734 -0.45728816  0.352889082  0.9868844505 -0.68401129

谁能帮我找出错误?

推荐答案

要使矩阵乘法起作用,您需要将 data.frame(大概就是 da 的意思)转换为矩阵:

To get the matrix multiplication to work, you need to convert the data.frame (presumably that's what da is) to a matrix:

t(da)%*%as.matrix(da)

但这给出了一个 7x7 矩阵,它不能添加到您正在使用的 3x3 单位矩阵中.你的意思是这样的:

But this gives a 7x7 matrix which can't be added to the 3x3 identity matrix that you're using. Do you mean something like:

ma=diag(7)+t(da)%*%as.matrix(da)

您可能想看看 R 简介 如果您对矩阵和 data.frame 之间的区别没有信心.

You may like to have a look at An Introduction to R if you don't feel confident about the difference between a matrix and data.frame.

这篇关于矩阵表达式导致错误“需要数字/复杂矩阵/向量参数"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆