Help us understand the problem. What is going on with this article?

Rの行列とベクトル計算

More than 1 year has passed since last update.

よく使うやつをまとめておく

データ準備

> a <- c(2, 5)
> b <- c(3, 7)
> X <- matrix(1:4, nrow=2, ncol=2)
> Y <- matrix(5:8, nrow=2, ncol=2)

> print(a)
[1] 2 5
> print(b)
[1] 3 7
> print(X)
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> print(Y)
     [,1] [,2]
[1,]    5    7
[2,]    6    8

ベクトル と ベクトル

ベクトル同士の積(ベクトルの内積) %*%

> print(a %*% b)
     [,1]
[1,]   41

ベクトル同士の要素積 *

> print(a * b)
[1]  6 35

ベクトル同士の直積(ベクトルのテンソル積) %o%

> print(a %o% b)
     [,1] [,2]
[1,]    6   14
[2,]   15   35

行列 と 行列

行列同士の積 %*%

> print(X %*% Y)
     [,1] [,2]
[1,]   23   31
[2,]   34   46

行列同士の要素積 *

> print(X * Y)
     [,1] [,2]
[1,]    5   21
[2,]   12   32

行列同士のクロネッカー積 %x%

> print(X %x% Y)
     [,1] [,2] [,3] [,4]
[1,]    5    7   15   21
[2,]    6    8   18   24
[3,]   10   14   20   28
[4,]   12   16   24   32

行列とベクトル

行列とベクトルの積 %*%

> X %*% a
     [,1]
[1,]   17
[2,]   24

行列とベクトルの要素積(わかりづらい) *

> X * a
     [,1] [,2]
[1,]    2    6
[2,]   10   20
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away