1-dimensional Matrix is changed to a vector in R

前端 未结 2 570
余生分开走
余生分开走 2020-12-06 10:45
> a<-matrix(c(1:9),3,3)
> a
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> a[3,]*a[,3]  # I expect 1x1 matrix as resul         


        
相关标签:
2条回答
  • 2020-12-06 11:18

    This is an R FAQ. You need to do a[3,,drop = FALSE].

    0 讨论(0)
  • 2020-12-06 11:29

    You're confusing element-by-element multiplication and matrix multiplication (see ?"*"). You want %*%:

    > a[3,]%*%a[,3]
         [,1]
    [1,]  150
    
    0 讨论(0)
提交回复
热议问题