矩阵计算学习日记(1)(未完待续)

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
dimnames = NULL)

as.matrix(x, …)

S3 method for class ‘data.frame’

as.matrix(x, rownames.force = NA, …)

is.matrix(x)

data
an optional data vector (including a list or expression vector). Non-atomic classed R objects are coerced by as.vector and all attributes discarded.

nrow
the desired number of rows.

ncol
the desired number of columns.

byrow
logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.

dimnames
A dimnames attribute for the matrix: NULL or a list of length 2 giving the row and column names respectively. An empty list is treated as NULL, and a list of length one as row names. The list can be named, and the list names will be used as names for the dimensions.

x
an R object.


additional arguments to be passed to or from methods.

rownames.force
logical indicating if the resulting matrix should have character (rather than NULL) rownames. The default, NA, uses NULL rownames if the data frame has ‘automatic’ row.names or for a zero-row data frame.

练习:
(1)生成两个矩阵 A 3 ∗ 4 A_{3*4} A34 B 4 ∗ 3 B_{4*3} B43,元素分别来自N(-1,1)和N(0,1)
(2)计算2A+3B^T
(3)计算C=AB

R代码

set.seed(11)
 (A=matrix(rnorm(12,-1,1),3,4))#生成一个3*4的矩阵,元素来自于正态分布N(-1,1)
           [,1]       [,2]       [,3]      [,4]
[1,] -1.5910311 -2.3626533  0.3236056 -2.004121
[2,] -0.9734056  0.1784892 -0.3750822 -1.828433
[3,] -2.5165531 -1.9341513 -1.0457230 -1.348352

(B=matrix(rnorm(12,0,1),4,3))
            [,1]       [,2]        [,3]
[1,] -1.53829340 -0.2229695 -0.68251762
[2,] -0.25556525  0.8877716 -0.01585819
[3,] -1.14994503 -0.5921553 -0.44260479
[4,]  0.01232697 -0.6557181  0.35255750

 (2*A+3*t(B))  ##  t() 矩阵转置
          [,1]      [,2]      [,3]      [,4]
[1,] -7.796942 -5.492002 -2.802624 -3.971260
[2,] -2.615720  3.020293 -2.526630 -5.624021
[3,] -7.080659 -3.915877 -3.419260 -1.639031

(C=A%*%B)   ##%*%矩阵相乘
         [,1]       [,2]      [,3]
[1,] 2.654451 -0.6202318 0.2735770
[2,] 1.860553  1.7965411 0.1829213
[3,] 5.551402  0.3473990 1.7357345

python代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值