矩阵计算学习日记(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代码

你可能感兴趣的:(线性代数,机器学习,矩阵)