r - 在 R 中创建或初始化一个空矩阵
问题描述
我正在使用R v 3.0.0 (2013-04-03)
并RStudio v 1.1.463
在 Win-7 64 位下使用。
在以下源代码中:
# Problem 1 - Matrix powers in R
#
# R does not have a built-in command for taking matrix powers.
# Write a function matrixpower with two arguments mat and k that
# will take integer powers k of a matrix mat.
matrixMul <- function(mat1)
{
rows <- nrow(mat1)
cols <- ncol(mat1)
matOut = matrix(, nrow = rows, ncol = cols) # empty matrix
for (i in 1:rows)
{
for(j in 1:cols)
{
vec1 <- mat1[i,]
vec2 <- mat1[,j]
mult1 <- vec1 * vec2
matOut[i,j] <- mult1
}
}
return(matOut)
}
matrixpower<-function(mat1, k)
{
matOut <-mat1#empty matix
for (i in k)
{
matOut <- matrixMul(matOut)
}
return(matOut)
}
mat1 <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, ncol=3)
power1 <- matrixMul(mat1)
宣言
matOut <- matrix(, nrow = rows, ncol = cols) # empty matrix
甚至在编译之前给出以下语法错误:
missing argument to function call
我正在遵循这些说明。
我在这里做错了什么?
解决方案
试试这个:
matOut = matrix(numeric(rows*cols), nrow = rows, ncol = cols) # empty matrix
推荐阅读
- python - 当我必须在长熊猫系列(> 15 M 个元素)中搜索时,如何优化“枚举”?
- python - setuptools-scm:当前版本而不是下一个版本
- dataframe - pyspark中的collect()是否有另一种方法?py4j.protocol.Py4JJavaError: 调用 0323.collectToPython 时出错
- matlab - 如何仅分析信号中的脉冲(峰值)?
- javascript - 为什么代码“水平循环滚动”在真正的浏览器上不起作用?
- javascript - Firestore“设置”和文档的最大写入速率
- javascript - 如何根据值更改标签中文本的颜色
- c - 计算 h264 rtp 流的 pps 时出错
- mongodb - 在 { $match: { $text: { $search: search } } 之后 Mongo 不使用索引吗?
- php - 致命错误:未捕获错误:使用 phpGACL 时调用未定义函数 mysql_pconnect()