1
0

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 5 years have passed since last update.

Octave メモ

Last updated at Posted at 2020-05-06

概要

ちょっとした数値シミュレーションをする必要が発生した。
何度も使うのなら Matlab を手配するんだが、多分単発。
というわけで Octave でやることに。
コマンドの個人的なメモ

コマンドメモ

空行列を作る

data =[]

ゼロ行列を作る

data =zeros(20,5)

行列からランダムな要素を出す

data = rand(10,4); %サンプルデータ作成(10行4列)
idx = randperm(10,2); %ランダムな行インデックスを作成
data_sub = data(idx,:); %2行分のランダムサンプリング

ファイルからロード

sample.m
[X, Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R) ./ R;
figure
mesh(X, Y, Z)

拡張子が*.m*のものをスクリプトファイルとして読み込む。このサンプルはgnuoplotを呼び出すので、ssh接続した先だと使えないので注意。

octave:1> pwd
ans = /home/shakapon
octave:2> ls ./sample.m
./sample.m
octave:3> sample

コマンドラインからの入出力

octave:1> a=input("INPUT:a=");
INPUT a= 10 %← 10と入力
a =  10
octave:2> disp(a)
 10

単にaと変数と入力した場合とdisp関数を使った場合の違いは式となるかならないか、らしい。

条件分岐

if (COND)
    ...
elseif (COND)
    ...
else
    ...
end

繰り返し

for x = ベクトル
    ...
end

演算記号一覧

演算記号 概要
input input(’n=’) 文字の入力
save save(’ ファイル名’) A; 変数のバックアップ
clear clear A 変数Aの破壊(clear のみだと全ての変数の破壊)
load load ’ ファイル名’ 修復
who who 定義した変数の確認
fopen a=fopen(’file.txt’,’w+’) file.txt を w+として開く。
fclose fclose(a) ファイルを閉じる
fprintf fprintf(txt ファイル,%f, 行列) 行列の出力変換を txt ファイルに出力

CPU時間

[total, user,system]=cputime();

実行中のセッションで使用されたCPUの時間

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?