Octave共通文コマンド
1. Octaveの紹介
Octave は MATLAB と非常によく似たソフトウェアです。OctaveがMATLABより優れている点は,オープンソースで無料であることと,比較的規模が小さいことです。両者の構文の違いは十分小さく,片方で実行できるプログラムは,少し修正するだけでもう片方でも実行できます。以下は,いくつかの一般的なOctaveの文とコマンドです。
2. 一般的なステートメントコマンド
2.1 四則演算
Doing quadratic operations with Octave is relatively basic, and is similar to the common use of scientific calculators.
2.2 論理演算
"1 == 1" % Determine equality
"1 ~= 2" % Note the inequality sign
"1 && 0" % Find the logical sum
"xor(1, 0)" % find the dissimilarity, note that this is another form of syntax
2.3 変数表現
a = 3
a = 3; % The difference is in the semicolon, which is printed immediately afterwards without it
disp(a) % prints the variable a
a = 3.141593;
disp(sprintf("2 decimals : %0.2f), a) % Format control, similar to C style, prints out to two decimal places
>> format long
>> a = pi
a = 3.14159265358979
>> format short
>> a = pi
a = 3.1416
% precision is different
>>
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6 % Build the matrix
>> V = [1 2 3]
V =
1 2 3 % Create row vector
>>
>> V = [1; 2; 3] % Create column vector
V =
1
2
3
>> V = 1: 0.1: 2 % is more intuitive, with a step size of 0.1, creating a row vector
V =
Columns 1 through 8:
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000
Columns 9 through 11:
1.8000 1.9000 2.0000
>> V = 1:6 % is obviously the default step size of 1, omitted, and consistent with the previous
V =
1 2 3 4 5 6
>> ones(2, 3) % Other methods for generating matrices
ans =
1 1 1 1
1 1 1
>> zeros(2, 3) % Generate a zero matrix
ans =
0 0 0 0
0 0 0 0
>> rand(2, 3) % take a random number, define the dimension, the size of the random number is always between 0 and 1
ans =
0.57457 0.79732 0.29826
0.61112 0.51547 0.88642
>> randn(2, 3) % randn indicates that the random numbers are from a standard normal distribution
ans =
0.897613 0.063959 -0.838812
0.205952 -1.267830 -1.060730
>> w = randn(1, 10000);
>> hist(w) % histogram command
>> eye(4) % get unit matrix
ans =
Diagonal Matrix
1 0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0 1
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A) % get matrix dimension
ans =
3 2
>>
>> size(A, 1) % Get the number of rows
ans = 3
>> size(A, 2) %Get the number of columns
ans = 2
>>
>> V = [1 2 4 5 6];
>> length(V) % Get the length of the vector
ans = 5
>> A = [1 2; 3 4; 5 6;];
>> A(3,2) % Get an element by index
ans = 6
>> A(2, :) % Get a row of elements directly, where a colon indicates all elements of a row or column
ans =
3 4
>> A([1 2], :) % return all elements of the first and second rows
ans =
1 2
3 4
>> v = [11; 22; 33];
>> A = [A, v] % Append a column vector to A
A =
1 2 11
3 4 22
5 6 33
>> A(:) % has a special meaning, the result is that all elements of A are placed in a column vector
ans =
1
3
5
2
4
6
11
22
33
>> A = [1 2; 3 4; 5 6;];
>> B = [1 2; 3 4; 5 6;];
>> C = [A B] % "horizontally" connected
C =
1 2 1 2
3 4 3 4
5 6 5 6
>> C = [A; B] % "vertically" connected
C =
1 2
3 4
5 6
1 2
3 4
5 6
2.4 ベクトルとマトリックス
>> A = [1 2; 3 4; 5 6;];
>> B = [11 12; 13 14; 15 16];
>> C = [1 2; 3 4];
>> A*C % regular matrix multiplication
ans =
7 10
15 22
23 34
>> A.*B % Special matrix multiplication: corresponding elements are multiplied together
ans = % Then there are . ^ and . / have the same reasoning
11 24
39 56
75 96
>> v = [1; 2; 3];
>> log(v) % logarithm for each element of the column vector
ans =
0.00000
0.69315
1.09861
>> exp(v) % find the exponent
ans =
2.7183
7.3891
20.0855
>> abs(v) % find the absolute value
ans =
1
2
3
>> v = v + ones(length(v), 1) % A concise way to add 1 to each element of the column vector
v =
2
3
4
>> v = [1 15 2 5];
>> v < 3 % Make logical judgments for each element
ans =
1 0 1 0
>> find(v<3) % Return the index of the result as true based on the logical judgement above
ans =
1 3
>> max(max(A))
ans = 9
>> max(A(:)) % Two ways to find the maximum of all elements in a matrix
ans = 9
sum(A); find the sum of each column in matrix A
sum(A, 1); as above
sum(A, 2); find the sum of each row
sum(sum(A)); this gives the sum of all elements of the matrix
sum(sum(eye(3) . * A)); get the sum of the main diagonal elements
sum(sum(A.*flipud(eye(3 )))); get the sum of the subdiagonal elements
pinv(A); if A is a singular matrix, get the inverse of A. If A is a non-singular matrix or not a square matrix, get the generalized inverse of A
>> y1 = sin(2*pi*4*t);
>>> plot(t,y1); % plot the image
>> plot(t,y1)
>> hold on; % Note the hold on command, which draws the other two images in the same image at the same time
>> plot(t,y2,'r');
>> xlabel('time'); % label the x-axis
>> ylabel('value'); % label the y-axis
>> legend('sin','cos'); % add legend, legend has the meaning of "legend"
>> title('My Plot'); % Add the title
>> print -dpng 'myPlot'; % save the image as a png in the default path
>> close % Close the image
>> figure(1);plot(t,y1); % plot in figure1
>> figure(2);plot(t,y2); % plot in figure2, otherwise the previous figure will be closed and the figure will be plotted in figure1
>> subplot(N,M,index); plot(t,y2);% divide an image into NxM copies, this time in the index plot
axis([0.5 1 -1 1]); % set the coordinate draw interval
clf % erase the image, but do not close the figure
imagesc(magic(15)), colorbar, colormap gray;% plot the matrix in grayscale, the key to know here is to use the "comma linker", enter three commands at once to execute
for i = 1:10
v = i * 2;
end
% Two ways to write this, much the same, semicolon or comma or no symbol, both work
indices = 1:10;
>> for i=indices,
v(i) = i*i;
end;
The % while loop is written
i = 1;
>> while i <= 5;
v(i) = 100;
i = i+1;
end;
Usage of %break statement
>> i = 1;
>> while true;
v(i) = 999;
i = i+1;
if i == 6;
break;
end;
end;
% if-else statement usage examples
>> a = 1
a = 1
>> if a == 1,
disp('The value is one');
elseif a == 2,
disp('The value is two');
else
disp('whocare');
end;
The value is one
Two examples of the use of the % function
function [a,b] = CubeFunc(x)
a = x^3 ; b = x^4; % with multiple return values
A simple implementation of %Cost Function
function J = CostFunc(x,y,theta)
m = length(y);
var = y - X * theta;
sqrError = sum(var . ^2);
J = 1/(2*m)*sqrError;
% Try to reflect the formula when writing the function, it is not too troublesome and less error-prone
>> rand(2, 3) % take a random number, define the dimension, the size of the random number is always between 0 and 1
ans =
0.57457 0.79732 0.29826
0.61112 0.51547 0.88642
>> randn(2, 3) % randn indicates that the random numbers are from a standard normal distribution
ans =
0.897613 0.063959 -0.838812
0.205952 -1.267830 -1.060730
>> w = randn(1, 10000);
>> hist(w) % histogram command
<イグ
>> eye(4) % get unit matrix
ans =
Diagonal Matrix
1 0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0 1
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A) % get matrix dimension
ans =
3 2
>>
>> size(A, 1) % Get the number of rows
ans = 3
>> size(A, 2) %Get the number of columns
ans = 2
>>
>> V = [1 2 4 5 6];
>> length(V) % Get the length of the vector
ans = 5
>> A = [1 2; 3 4; 5 6;];
>> A(3,2) % Get an element by index
ans = 6
>> A(2, :) % Get a row of elements directly, where a colon indicates all elements of a row or column
ans =
3 4
>> A([1 2], :) % return all elements of the first and second rows
ans =
1 2
3 4
>> v = [11; 22; 33];
>> A = [A, v] % Append a column vector to A
A =
1 2 11
3 4 22
5 6 33
>> A(:) % has a special meaning, the result is that all elements of A are placed in a column vector
ans =
1
3
5
2
4
6
11
22
33
>> A = [1 2; 3 4; 5 6;];
>> B = [1 2; 3 4; 5 6;];
>> C = [A B] % "horizontally" connected
C =
1 2 1 2
3 4 3 4
5 6 5 6
>> C = [A; B] % "vertically" connected
C =
1 2
3 4
5 6
1 2
3 4
5 6
2.5 データの読み込み
pwd指示文:Octaveのデフォルトパスを取得する。
cdコマンド:方向を変える、つまりパスを変更する、パスを自分で指定する、ただしパス名に漢字を含まないこと
ls コマンド:デフォルトパスに含まれるすべてのパスまたはファイルをリストアップします。
load test.dat : ファイルを読み込むコマンドです。
load('test.dat) : 上記と同様です。
test : test はコマンドではなくファイル名で、ロードされた test という名前のファイルを示しています。
size(test) : テストファイルの寸法を取得します。
who/whos : Octaveワークスペースに現在含まれているすべての変数をリストアップします。
clear test : test はファイル名であり、ワークスペースから変数 test を削除する。
v = test(1 : 10) : testの最初の10個の値を取り、変数vに代入する。
save hello.dat v : 変数 v を hello.dat ファイルに保存し、再度読み込んだときに変数名が hello ではなく v になることに注意する。
2.6 データを計算する
>> A = [1 2; 3 4; 5 6;];
>> B = [11 12; 13 14; 15 16];
>> C = [1 2; 3 4];
>> A*C % regular matrix multiplication
ans =
7 10
15 22
23 34
>> A.*B % Special matrix multiplication: corresponding elements are multiplied together
ans = % Then there are . ^ and . / have the same reasoning
11 24
39 56
75 96
>> v = [1; 2; 3];
>> log(v) % logarithm for each element of the column vector
ans =
0.00000
0.69315
1.09861
>> exp(v) % find the exponent
ans =
2.7183
7.3891
20.0855
>> abs(v) % find the absolute value
ans =
1
2
3
>> v = v + ones(length(v), 1) % A concise way to add 1 to each element of the column vector
v =
2
3
4
val = max(v) : 列ベクトル中の最大の値を返し,インデックスも与える.
val = max(A) : 行列の各列の最大の要素を返すので、Aがm*n次元で繰り返しがない場合は、1*n次元を返します。
>> v = [1 15 2 5];
>> v < 3 % Make logical judgments for each element
ans =
1 0 1 0
>> find(v<3) % Return the index of the result as true based on the logical judgement above
ans =
1 3
>> max(max(A))
ans = 9
>> max(A(:)) % Two ways to find the maximum of all elements in a matrix
ans = 9
sum(A); find the sum of each column in matrix A
sum(A, 1); as above
sum(A, 2); find the sum of each row
sum(sum(A)); this gives the sum of all elements of the matrix
sum(sum(eye(3) . * A)); get the sum of the main diagonal elements
sum(sum(A.*flipud(eye(3 )))); get the sum of the subdiagonal elements
pinv(A); if A is a singular matrix, get the inverse of A. If A is a non-singular matrix or not a square matrix, get the generalized inverse of A
2.7 Octaveで画像をプロットする
>> y1 = sin(2*pi*4*t);
>>> plot(t,y1); % plot the image
>> plot(t,y1)
>> hold on; % Note the hold on command, which draws the other two images in the same image at the same time
>> plot(t,y2,'r');
>> xlabel('time'); % label the x-axis
>> ylabel('value'); % label the y-axis
>> legend('sin','cos'); % add legend, legend has the meaning of "legend"
>> title('My Plot'); % Add the title
>> print -dpng 'myPlot'; % save the image as a png in the default path
>> close % Close the image
>> figure(1);plot(t,y1); % plot in figure1
>> figure(2);plot(t,y2); % plot in figure2, otherwise the previous figure will be closed and the figure will be plotted in figure1
>> subplot(N,M,index); plot(t,y2);% divide an image into NxM copies, this time in the index plot
axis([0.5 1 -1 1]); % set the coordinate draw interval
clf % erase the image, but do not close the figure
imagesc(magic(15)), colorbar, colormap gray;% plot the matrix in grayscale, the key to know here is to use the "comma linker", enter three commands at once to execute
2.8 制御文
for i = 1:10
v = i * 2;
end
% Two ways to write this, much the same, semicolon or comma or no symbol, both work
indices = 1:10;
>> for i=indices,
v(i) = i*i;
end;
The % while loop is written
i = 1;
>> while i <= 5;
v(i) = 100;
i = i+1;
end;
Usage of %break statement
>> i = 1;
>> while true;
v(i) = 999;
i = i+1;
if i == 6;
break;
end;
end;
% if-else statement usage examples
>> a = 1
a = 1
>> if a == 1,
disp('The value is one');
elseif a == 2,
disp('The value is two');
else
disp('whocare');
end;
The value is one
Two examples of the use of the % function
function [a,b] = CubeFunc(x)
a = x^3 ; b = x^4; % with multiple return values
A simple implementation of %Cost Function
function J = CostFunc(x,y,theta)
m = length(y);
var = y - X * theta;
sqrError = sum(var . ^2);
J = 1/(2*m)*sqrError;
% Try to reflect the formula when writing the function, it is not too troublesome and less error-prone
関連
-
Solve ImportError: cannot import name 'AipOcr' from 'aip'
-
ImportError: libGL.so.1: 共有オブジェクトファイルを開くことができません。
-
カーネル再始動
-
mac が正常に解決しました AttributeError: module 'enum' has no attribute 'IntFlag'?
-
ValueError: ubuntu の pycharm で matplotlib をインストールすると、 max() arg が空のシーケンスになる。
-
ImportError: scipy.sparseという名前のモジュールはありません。
-
Ubuntu 18.04で深層学習フレームワークPytorch(GPUアクセラレーション)をインストールする。
-
ロジスティック回帰のエラー問題:警告メッセージ。1: glm.fit: アルゴリズムが集約されていない 2: glm.fit: 適合率が0か1の値で計算されている
-
第1章 ニューラルネットを用いた手書き数字の認識
-
Neo4jのインストールと簡単な使い方
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
Error: cudaGetDevice() failed. Status: CUDAドライババージョンがCUDAランタイムバージョンに対して不十分です。
-
Keras、TensorFlowのインポート時にTensorFlowのネイティブランタイムのロードに失敗しました。
-
tensorflowに一致するディストリビューションは見つかりませんでした。
-
Kerasを使ったテンソルの乗算と和算
-
[機械学習実践編 - ch09] TypeError: Unsupported operand type(s) for /: map' と 'int' です。
-
pip install インストール [WinError 10061] ターゲットコンピュータが積極的に拒否するため、接続できません。(Win10) Windows(10) pip install install [WinError 10061] ターゲットコンピュータによる積極的な拒否のため接続できません。
-
ERRORの解決方法 scipy のインストール時に Command errored out with exit status 1: が発生しました。
-
TensorFlowのインストールとアンインストール(anaconda版)
-
時系列モデル(ARIMA、ARMA)完全ステップバイステップ詳解
-
predict_proba, predict, decision_function の scikit-learn toolkit での使用法。