欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言矩陣知識點總結(jié)及實例分析

 更新時間:2021年04月11日 09:45:46   作者:w3cschool  
在本篇文章里小編給各位整理了一篇關(guān)于R語言矩陣知識點總結(jié)及實例分析,對此有興趣的朋友們可以學(xué)習(xí)下。

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

使用matrix()函數(shù)創(chuàng)建一個矩陣。

語法

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

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

以下是所使用的參數(shù)的說明

  • 數(shù)據(jù)是成為矩陣的數(shù)據(jù)元素的輸入向量。
  • 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","
")
print(result)

# Subtract the matrices
result <- matrix1 - matrix2
cat("Result of subtraction","
")
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","
")
print(result)

# Divide the matrices
result <- matrix1 / matrix2
cat("Result of division","
")
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語言矩陣知識點總結(jié)及實例分析的詳細內(nèi)容,更多關(guān)于R語言矩陣的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • R語言 中文亂碼的解決方案

    R語言 中文亂碼的解決方案

    這篇文章主要介紹了R語言 中文亂碼的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 使用R語言繪制3D數(shù)據(jù)可視化scatter散點圖實現(xiàn)步驟

    使用R語言繪制3D數(shù)據(jù)可視化scatter散點圖實現(xiàn)步驟

    這篇文章主要為大家介紹了使用R語言繪制3D數(shù)據(jù)可視化scatter散點圖的實現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • R語言字符串知識點總結(jié)及實例分析

    R語言字符串知識點總結(jié)及實例分析

    在本篇文章里小編給各位分享的是一篇關(guān)于R語言字符串知識點總結(jié)及實例分析,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • R語言繪制Facet violin plot小提琴刻面圖實現(xiàn)示例

    R語言繪制Facet violin plot小提琴刻面圖實現(xiàn)示例

    這篇文章主要為大家介紹了R語言繪制Facet violin plot小提琴刻面圖的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • R語言的一個加法函數(shù)使用介紹

    R語言的一個加法函數(shù)使用介紹

    這篇文章主要介紹了R語言的一個加法函數(shù)使用介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R包制作后出現(xiàn)not available for錯誤問題解決解決

    R包制作后出現(xiàn)not available for錯誤問題解決解決

    這篇文章主要為大家介紹了R包制作后出現(xiàn)not available for...錯誤的問題解決方式,有需要的朋友,可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • R語言運算符知識點總結(jié)

    R語言運算符知識點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于R語言運算符知識點總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-03-03
  • R語言是什么 R語言簡介

    R語言是什么 R語言簡介

    R是用于統(tǒng)計分析、繪圖的語言和操作環(huán)境。R是屬于GNU系統(tǒng)的一個自由、免費、開源的軟件,它是一個用于統(tǒng)計計算和統(tǒng)計制圖的優(yōu)秀工具
    2021-03-03
  • R語言正態(tài)分布的實現(xiàn)示例

    R語言正態(tài)分布的實現(xiàn)示例

    R語言中正態(tài)分布包括四個主要函數(shù):rnorm、dnorm、pnorm、qnorm,分別用于生成隨機數(shù)、計算概率密度、累積概率和計算分位數(shù),本文就來詳細的介紹一下具體用法,感興趣的可以了解一下
    2024-10-10
  • R語言關(guān)于決策樹知識點總結(jié)

    R語言關(guān)于決策樹知識點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于R語言關(guān)于決策樹知識點總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05

最新評論