Octave 関数

abs

abs(-1)
ans = 1

all

% すべての配列の要素が非0かTRUEかを返す
x = [1 1 1; 1 1 0]

x =

1 1 1
1 1 0

% 列単位で返す
all(x, 1)

ans =

1 1 0

% 行単位で返す
all(x, 2)

ans =

1
0

any

% 配列の要素が非0かどうかを返す
x = [1 0 1; 0 0 0]

x =

1 0 1
0 0 0

% 列単位で返す
any(x, 1)

ans =

1 0 1

% 行単位で返す
any(x, 2)

ans =

1
0

assert

% falseならエラーを出力する
assert(0)

error: assert (0) failed
error: called from:
error: /usr/share/octave/3.6.4/m/testfun/assert.m at line 72, column 1

corr

% 相関係数
x = [1 2 3]

x =

1 2 3

y = [2 4 6]
y =

2 4 6

corr(y, x)
ans = 1

 corrcoefという関数もあるが、Matlabのcorrcoefとは同じではなく、今後のOctaveでは削除されるらしい。

 なお、Matlabにもcorr関数はある。

cumprod

% 累積積
x = 1:5

x =

1 2 3 4 5

cumprod(x)
ans =

1 2 6 24 120

datenum

% 日付をシリアル日付値に変換
datenum('20131101', 'yyyymmdd')

ans = 735539

datestr

% シリアル日付値を日付に変換
datenum('20131101', 'yyyymmdd')

ans = 735539

datestr(735539)
ans = 01-Nov-2013

diag

diag([1 2 3])
ans =

Diagonal Matrix

1 0 0
0 2 0
0 0 3

false

% すべての要素が0の配列を返す
false(2, 3)

ans =

0 0 0
0 0 0

find

x = randperm(5)
x =

4 2 1 5 3

find(x > 2)
ans =

1 4 5

fminbnd

% 指定された範囲で関数が最小となる値を返す
y = inline('x^2')

y = f(x) = x^2
fminbnd(y,-3,-2)
ans = -2.0000

 y=x^2という関数で-3<=x<=-2の範囲では、yが最小となるのはx=-2のとき(y=4)なので、上の例では-2を返す。

fprintf

% データを画面に表示
fprintf("test\n")

test

inline

% インライン関数の作成
y = inline('x^2')

y = f(x) = x^2
y(3)
ans = 9

 MATLABではこの関数は削除予定とのこと。

intersect

% 2つの配列に共通なものを返す
x = 1:3

x =

1 2 3

y = 2:4
y =

2 3 4

intersect(x, y)
ans =

2 3

isfinite

% 有限値なら1、無限値またはNaNなら0を返す
x = [1 1/0]

x =

1 Inf

isfinite(x)
ans =

1 0

isnan

% NaNの要素があれば1を、さもなければ0を返す
x = [1 NaN]

x =

1 NaN

isnan(x)
ans =

0 1

kurtosis

% 尖度
x = 1:5

x =

1 2 3 4 5

kurtosis(x)
ans = -1.9120

length

% ベクトルの長さ
x = 1:5

x =

1 2 3 4 5

length(x)
ans = 5

log

% 自然対数
log(10)

ans = 2.3026

logical

% 数値を論理値に変換
x = 0:2

x =

0 1 2

logical(x)
ans =

0 1 1

max

% 最大値
max(2, 3)

ans = 3

mean

% 平均
x = 1:10

x =

1 2 3 4 5 6 7 8 9 10

mean(x)
ans = 5.5000

min

% 最小値
min(2, 3)

ans = 2

NaN

% すべての要素がNaNの配列を返す
NaN(2, 3)

ans =

NaN NaN NaN
NaN NaN NaN

NaN(1, 2, 3)
ans =

ans(:,:,1) =

NaN NaN

ans(:,:,2) =

NaN NaN

ans(:,:,3) =

NaN NaN

ones

% すべての要素が1の配列を返す
ones(2, 3)

ans =

1 1 1
1 1 1

randperm

% ランダム置換
randperm(5)

ans =

4 5 2 1 3

repmat

x = [1 2 3]
x =

1 2 3

repmat(x, 2, 3)
ans =

1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3

round

round(0.5)
ans = 1

setdiff

% 第1引数の要素で第2引数にないものを返す
x = 1:3

x =

1 2 3

y = 3:5
y =

3 4 5

setdiff(x, y)
ans =

1 2

sign

% 正なら1、負なら-1を返す
sign(2)

ans = 1

sign(-3)
ans = -1

size

 size()関数は配列の行数、列数を返す。

x = zeros(2, 3)
x =

0 0 0
0 0 0

size(x)
ans =

2 3
size(x, 1)
ans = 2
size(x, 2)
ans = 3

skewness

% 歪度
x = 1:5

x =

1 2 3 4 5

skewness(x)
ans = 0

sort

x = randperm(5)
x =

5 1 4 2 3

sort(x, 'ascend')
ans =

1 2 3 4 5

sqrt

sqrt(10)
ans = 3.1623

std

% 標準偏差
x = 1:5

x =

1 2 3 4 5

std(x)
ans = 1.5811

sum

x = [1 2 3]
x =

1 2 3

sum(x)
ans = 6

x = [1:3; 4:6]
x =

1 2 3
4 5 6

sum(x, 1)
ans =

5 7 9

sum(x, 2)
ans =

6
15

weekday

datenum('20131101', 'yyyymmdd')
ans = 735539

% 曜日を返す
% 6は金曜日
weekday(735539)

ans = 6

zeros

% すべての要素が0の配列を返す
zeros(2, 3)

ans =

0 0 0
0 0 0

コメントの投稿

非公開コメント

プロフィール

Author:fxst24
自分用のノートとして作っていますが、同学の方々の参考になれば幸いです。
記事はことわりなく修正、削除、非公開にすることがあります。

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
QRコード
QR