バックナンバーはこちら。
https://www.simulationroom999.com/blog/compare-matlabpythonscilabjulia3-backnumber/
はじめに
非極大値抑制をプログラムを作成しているときに出てきた、
線形インデックスサーチと論理インデックスサーチについて説明。
今回は、各ツール、各言語で線形インデックスサーチを確認。
登場人物
博識フクロウのフクさん

イラストACにて公開の「kino_k」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=iKciwKA9&area=1
エンジニア歴8年の太郎くん

イラストACにて公開の「しのみ」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=uCKphAW2&area=1
各ツールで線形インデックスサーチ

太郎くん
今回は、各ツール、各言語で確認だね。

フクさん
まずは、線形インデックスサーチ。
一気に確認してしまおう。
MATLAB
>> X=[0.1 0.2 0.3 0.4 0.5]
X =
0.1000 0.2000 0.3000 0.4000 0.5000
>> find(0.4<=X)
ans =
4 5
>> X(find(0.4 <= X))
ans =
0.4000 0.5000
>> X(find(0.4<=X))=0
X =
0.1000 0.2000 0.3000 0 0
Python(NumPy)
>>> import numpy as np
>>> X=np.array([0.1, 0.2, 0.3, 0.4, 0.5])
>>> np.where(0.4<=X)
(array([3, 4], dtype=int64),)
>>> X[np.where(0.4<=X)]
array([0.4, 0.5])
>>> X[np.where(0.4<=X)]=0
>>> X
array([0.1, 0.2, 0.3, 0. , 0. ])
Scilab
--> X=[0.1 0.2 0.3 0.4 0.5]
X =
0.1 0.2 0.3 0.4 0.5
--> find(0.4<=X)
ans =
4. 5.
--> X(find(0.4 <= X))
ans =
0.4 0.5
--> X(find(0.4<=X))=0
X =
0.1 0.2 0.3 0. 0.
Julia
julia> X=[0.1 0.2 0.3 0.4 0.5]
1×5 Matrix{Float64}:
0.1 0.2 0.3 0.4 0.5
julia> findall(0.4.<=X)
2-element Vector{CartesianIndex{2}}:
CartesianIndex(1, 4)
CartesianIndex(1, 5)
julia> X[findall(0.4.<=X)]
2-element Vector{Float64}:
0.4
0.5
julia> X[findall(x-> 0.4 <= x, X)].=0
2-element view(::Matrix{Float64}, CartesianIndex{2}[CartesianIndex(1, 4), CartesianIndex(1, 5)]) with eltype Float64:
0.0
0.0
julia> X
1×5 Matrix{Float64}:
0.1 0.2 0.3 0.0 0.0
考察

太郎くん
どのツール、言語も大体似た感じではあるんだねー。
まぁfindが、whereだったり、findallだったりで関数名の差はあるみたいだけど。

フクさん
基本的には同じことができると思って良いだろう。
まとめ

フクさん
まとめだよ。
- 各ツール、各言語で線形インデックスサーチを確認。
- 基本的にはどの環境でも実施可能。
- MATLABのfind相当の関数がwhereだったりfindallだったりする程度の差はある。
バックナンバーはこちら。
コメント