在R中创建或初始化一个空矩阵

时间:2019-04-24 18:50:21

标签: r matrix

我正在Win-7 64位下使用R v 3.0.0 (2013-04-03)RStudio v 1.1.463

在以下源代码中:

# 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

enter image description here

我正在关注these instructions

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

尝试一下:

JNINativeMethod