0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

知っていて当たり前-09 ベクトル

Posted at

知っていて当たり前-09 ベクトル

1. ゼロベクトル zeros()

zeros(5)
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0
zeros(Int64, 5)
5-element Vector{Int64}:
 0
 0
 0
 0
 0

2. 1 ベクトル ones()

ones(5)
5-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
ones(Int8, 5)
5-element Vector{Int8}:
 1
 1
 1
 1
 1

3. 任意の要素を持つベクトル fill

fill(1.2, 5)
5-element Vector{Float64}:
 1.2
 1.2
 1.2
 1.2
 1.2

4. ビットベクトル trues(), falses()

trues(2)
2-element BitVector:
 1
 1
falses(2)
2-element BitVector:
 0
 0

5. 初期化しないベクトル Array{T}(undef, n)

Array{Int}(undef, 2)
2-element Vector{Int64}:
 4513206280
 4513206280

6. 型・要素数が既存のベクトルと同じ初期化されていないベクトル

a = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3
b = similar(a)
3-element Vector{Int64}:
 32
  4
  1

7. 等差数列

7.1. 階差(増分)を指定する場合 range(begin, end, step)

range(10, 20, step=3), 10:3:20
(10:3:19, 10:3:19)
collect(range(10, 20, step=3))
4-element Vector{Int64}:
 10
 13
 16
 19

7.2. 長さを指定する場合 range(begin, end, length)

range(10, 20, length=4)
10.0:3.3333333333333335:20.0
collect(10.0:3.3333333333333335:20.09)
4-element Vector{Float64}:
 10.0
 13.333333333333334
 16.666666666666668
 20.0

8. ベクトルのフラット化

x = [[1, 2, 3, 4, 5], [11, 12]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3, 4, 5]
 [11, 12]
vcat(x...)
7-element Vector{Int64}:
  1
  2
  3
  4
  5
 11
 12
a = [1, 2, 3]; b = [4, 5]; c = [6, 7, 8, 9];
[a, b, c]
3-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5]
 [6, 7, 8, 9]
vcat(a, b, c...)
9-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7
 8
 9

9. ベクトルを対象とする関数

9.1. 条件を満たすものの個数を返す count()

count(isequal(1), [2,1,2,1,1,3,4,2,1]) # 1 と等しいもの
4
count(iseven, [2,1,3,4,4,5,6,7,8]) # 偶数のもの
5
count(collect(1:20) .< 5) # 5 未満のもの
4

9.2. 一意の要素を返す unique()

ベクトル中に出現する(重複しない)要素を返す。結果はソートされてはいない。

unique([2,1,2,1,1,3,4,2,1])
4-element Vector{Int64}:
 2
 1
 3
 4
unique([2,1,2,1,1,3,4,2,1]) |> sort
4-element Vector{Int64}:
 1
 2
 3
 4
sort(unique([2,1,2,1,1,3,4,2,1]))
4-element Vector{Int64}:
 1
 2
 3
 4

9.3. ノルム norm()

$\displaystyle |A|p = \left( \sum{i=1}^n | a_i | ^p \right)^{1/p} $

using LinearAlgebra
v = [1.5, 2.3, 3.1, -5.2]
4-element Vector{Float64}:
  1.5
  2.3
  3.1
 -5.2
norm(v)
6.647555941848101
norm(v, 2)
6.647555941848101
norm(v, 1), sum(abs.(v))
(12.100000000000001, 12.100000000000001)
norm(v, 2), sum(abs.(v).^2) ^ (1/2), sqrt(sum(v.^2))
(6.647555941848101, 6.647555941848101, 6.647555941848101)
norm(v, 3), sum(abs.(v).^3) ^ (1/3), cbrt(sum(abs.(v).^3))
(5.707663847224675, 5.7076638472246755, 5.7076638472246755)
norm(v, Inf), maximum(abs.(v))
(5.2, 5.2)

9.4. 正規化 normalize()

using LinearAlgebra
v = [1.5, 2.3, 3.1, -5.2];
normalize(v, 1)
4-element Vector{Float64}:
  0.1239669421487603
  0.19008264462809912
  0.256198347107438
 -0.42975206611570244
v ./ norm(v, 1) # normalize() の定義
4-element Vector{Float64}:
  0.12396694214876032
  0.19008264462809912
  0.256198347107438
 -0.42975206611570244
normalize(v, 2)
4-element Vector{Float64}:
  0.22564684120326212
  0.3459918231783352
  0.46633680515340836
 -0.7822423828379753
v ./ norm(v, 2)
4-element Vector{Float64}:
  0.22564684120326212
  0.3459918231783352
  0.46633680515340836
 -0.7822423828379753

9.5. 内積 dot()

using LinearAlgebra
v = [1.5, 2.3, 3.1, -5.2];
w = [3, 6, 8, 3];
dot(v, w)
27.499999999999993
sum(v .* w) # dot() の定義
27.499999999999993

9.6. 外積

a = [1, 2, 3];
b = [10, 20, 30, 40];
a * b'
3×4 Matrix{Int64}:
 10  20  30   40
 20  40  60   80
 30  60  90  120

9.7. 二つの三要素ベクトルに対するクロス積 cross(), ×(x,y)

cross([1, 2, 3], [4, 5, 6]) # 二つの三要素ベクトルに対するクロス積
3-element Vector{Int64}:
 -3
  6
 -3
×([1, 2, 3], [4, 5, 6])
3-element Vector{Int64}:
 -3
  6
 -3
a1, a2, a3, b1, b2, b3 = 1, 2, 3, 4, 5, 6
(a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1)
(-3, 6, -3)

9.8. 回転シフト circshift()

arr = [1, 2, 3, 4, 5]
circshift(arr, 2)
5-element Vector{Int64}:
 4
 5
 1
 2
 3
circshift(arr, -2)
5-element Vector{Int64}:
 3
 4
 5
 1
 2
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?