在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ R語言矩陣
R語言列表
R語言隨機森林
R語言矩陣
R語言邏輯回歸
R語言數(shù)據(jù)幀
R語言數(shù)據(jù)重塑
R語言概述
R語言包
R語言字符串
R語言CSV文件
R語言運算符
為什么使用R語言做統(tǒng)計?
R語言Web數(shù)據(jù)
R語言二進制文件
R語言XML文件
R語言JSON文件
R語言因子
R語言容易學(xué)習(xí)嗎?
R語言基礎(chǔ)語法
R語言向量
R語言教程
R語言正態(tài)分布
R語言平均值,中位數(shù)和眾數(shù)
R語言變量
R語言條形圖
R語言決策樹
R語言開發(fā)環(huán)境安裝配置
R語言數(shù)組
R語言數(shù)據(jù)類型
R語言非線性最小二乘法
R語言直方圖
R語言卡方檢驗
R語言泊松回歸
R語言決策結(jié)構(gòu)
R語言盒形圖(箱形圖)
R語言協(xié)方差分析
R語言二項分布
R語言餅狀圖
R語言循環(huán)
R語言散點圖
R語言線性回歸
R語言時間序列分析
R語言線形圖
R語言在現(xiàn)實中的應(yīng)用
R語言生存分析
R語言多元(多重)回歸
R語言函數(shù)
R語言Excel文件
R語言連接數(shù)據(jù)庫(MySQL)

R語言矩陣

矩陣是其中元素以二維矩形布局排列的R對象。它們包含相同原子類型的元素。 雖然我們可以創(chuàng)建一個僅包含字符或僅包含邏輯值的矩陣,但它們沒有太多用處。 我們使用包含數(shù)學(xué)元素的矩陣來在數(shù)學(xué)計算中使用。

矩陣可通過使用matrix()函數(shù)來創(chuàng)建。

語法

在R中創(chuàng)建矩陣的基本語法是 -

matrix(data, nrow, ncol, byrow, dimnames)

以下是使用的參數(shù)的描述 -

  • data - 是將要轉(zhuǎn)為矩陣元素的輸入向量。
  • nrow - 是要創(chuàng)建的行數(shù)。
  • ncol - 是要創(chuàng)建的列數(shù)。
  • byrow - 是一個邏輯線索。 如果為TRUE,則輸入向量元素按行排列。
  • dimname - 是分配給行和列的名稱。

例子

創(chuàng)建一個使用數(shù)字向量作為輸入的矩陣,參考以下代碼 -

# Elements are arranged sequentially by row.
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)

# Elements are arranged sequentially by column.
N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)

# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
print(P)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

     [,1] [,2] [,3]
[1,]    3    4    5
[2,]    6    7    8
[3,]    9   10   11
[4,]   12   13   14
     [,1] [,2] [,3]
[1,]    3    7   11
[2,]    4    8   12
[3,]    5    9   13
[4,]    6   10   14
     col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14

訪問矩陣的元素

可以使用元素的列和行索引來訪問矩陣的元素。 我們考慮上面的矩陣P來找到下面的具體元素。

# Define the column and row names.
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

# Create the matrix.
P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

# Access the element at 3rd column and 1st row.
print(P[1,3])

# Access the element at 2nd column and 4th row.
print(P[4,2])

# Access only the  2nd row.
print(P[2,])

# Access only the 3rd column.
print(P[,3])

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1] 5
[1] 13
col1 col2 col3 
   6    7    8 
row1 row2 row3 row4 
   5    8   11   14

矩陣計算

使用R運算符對矩陣執(zhí)行各種數(shù)學(xué)運算,操作的結(jié)果也是一個矩陣。

操作中涉及的矩陣的尺寸(行數(shù)和列數(shù))應(yīng)相同。

矩陣加法與減法運算示例 -

# Create two 2x3 matrices.
matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)

# Add the matrices.
result <- matrix1 + matrix2
cat("Result of addition","\n")
print(result)

# Subtract the matrices
result <- matrix1 - matrix2
cat("Result of subtraction","\n")
print(result)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

     [,1] [,2] [,3]
[1,]    3   -1    2
[2,]    9    4    6
     [,1] [,2] [,3]
[1,]    5    0    3
[2,]    2    9    4
Result of addition 
     [,1] [,2] [,3]
[1,]    8   -1    5
[2,]   11   13   10
Result of subtraction 
     [,1] [,2] [,3]
[1,]   -2   -1   -1
[2,]    7   -5    2

矩陣乘法與除法運算示例

# Create two 2x3 matrices.
matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)

# Multiply the matrices.
result <- matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)

# Divide the matrices
result <- matrix1 / matrix2
cat("Result of division","\n")
print(result)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

     [,1] [,2] [,3]
[1,]    3   -1    2
[2,]    9    4    6
     [,1] [,2] [,3]
[1,]    5    0    3
[2,]    2    9    4
Result of multiplication 
     [,1] [,2] [,3]
[1,]   15    0    6
[2,]   18   36   24
Result of division 
     [,1]      [,2]      [,3]
[1,]  0.6      -Inf 0.6666667
[2,]  4.5 0.4444444 1.5000000

上一篇:R語言直方圖下一篇:R語言條形圖