【问题标题】:R: Function to determine if a Matrix is squareR:确定矩阵是否为正方形的函数
【发布时间】:2018-01-15 06:54:13
【问题描述】:

我正在尝试为 coursEra 上的 R 编程课程运行此示例。但是,当我尝试确定矩阵是否为正方形时,我收到错误消息“is.square.matrix(x) 中的错误:参数 x 不是矩阵”

我的代码如下:

library(matrixcalc)
##non-square matrix
NCols <- sample(3:6, 1)
NRows <- sample(2:8, 1)

myMat <- matrix(runif(NCols*NRows), ncol=NCols)
is.square.matrix(myMat)

## functions
makeMatrix <- function(x = matrix()) {
  m <- NULL
  set <- function(y) {
    x <<- y
    m <<- NULL
  }
  get <- function() x
  setInv <- if (is.square.matrix(x) == TRUE) {
    function(solve) m <<- solve
  }
     {
    function(ginv) m <<- ginv
  }
  getInv <- function() m
  list(x, set = set, get = get,
       setInv = setInv,
       getInv = getInv)
}

cacheMatrix <- function(x, ...) {
  m <- x$getInv()
  if(!is.null(m)) {
    message("getting cached data")
    return(m)
  }
  data <- x$get()
  m <-  if (is.square.matrix(x) == TRUE) {
    solve(data, ...)
  }
     {
    ginv(data, ...)
  }
  x$setInv(m)
  m
}

## run functions for matrix
notSquare <- makeMatrix(myMat) 
cacheMatrix(notSquare)


##check
ginv(myMat)

然后我得到错误:

is.square.matrix(x) 中的错误:参数 x 不是矩阵

我是初学者,所以不知道如何让 sentInv 识别并检查矩阵是否为正方形。

布赖恩

【问题讨论】:

    标签: r function if-statement


    【解决方案1】:

    这就是答案。我刚刚创建了函数 (x) 而不是函数 (x = matrix()) 并且“数据”是在缓存函数中拉取矩阵的变量,需要让我输入。

    ##non-square matrix
    NCols <- sample(3:6, 1)
    NRows <- sample(2:8, 1)
    
    myMat <- matrix(runif(NCols*NRows), ncol=NCols)
    is.square.matrix(myMat)
    
    ## functions
    makeCacheMatrix <- function(x) {
            m <- NULL
            set <- function(y) {
                    x <<- y
                    m <<- NULL
            }
            get <- function() x
            setInv <- if (is.square.matrix(x) == TRUE) {
                    function(solve) m <<- solve
            }
            else {
                    function(ginv) m <<- ginv
            }
            getInv <- function() m
            list(x, set = set, get = get,
                 setInv = setInv,
                 getInv = getInv)
    }
    
    cacheSolve <- function(x, ...) {
            m <- x$getInv()
            if(!is.null(m)) {
                    message("getting cached data")
                    return(m)
            }
            data <- x$get()
            m <-  if (is.square.matrix(data) == TRUE) {
                    solve(data, ...)
            }
            else {
                    ginv(data, ...)
            }
            x$setInv(m)
            m
    }
    
    ## run functions for myMat
    notSquare <- makeCacheMatrix(myMat) 
    cacheSolve(notSquare)
    
    
    ##check
    ginv(myMat)
    

    【讨论】:

      【解决方案2】:

      没关系。在 makeMatrix 函数中需要将 x 替换为 (x = matrix()) 并在 cacheMatrix 中将 x 替换为 (data)

      【讨论】:

      • 请发布更正后的代码。对于其他 SO 用户来说,这可能会很有趣。
      猜你喜欢
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      相关资源
      最近更新 更多