Octave 入门

常见命令及函数

disp()   % display

disp(sprintf('2 decimals: %.2f', pi))   % format output

format long   % set ‘long’ as default variable format

format short

A = [1 2; 3 4; 5 6]   % new a matrix

v = 1:0.1:2   % new a row vector from 1 to 2 with a interval of 0.1

k*ones(m, n)   % new a m by n matrix with all elements equaling to k

zeros(m, n)

rand(m,n)    % new a m by n matrix with random elements drawn from the uniform distribution between 0 and 1

randn()   % all the values drawn from a Gaussian distribution with mean 0 and variance or deviation equal to 1

w = -6 + sqrt(10)*(randn(1,10000))   % mean is -6, standard deviation is square root of 10

hist(w, 50)   % draw a histogram of 50 bins with w as the dataset

eye(4)   % new a 4 by 4 identity matrix

help   % how to use a command or a function

size(A)   % the size of a matrix, output another matrix

size(A, 1) or size (A, 2)

length(v)    % the size of the longest dimension

load('priceY.dat')
load featuresX.dat    % load data file 

who   % all the variables that Octave has in memory currently

whos    % that gives you the detailed view

clear   % clear all the variables in memory

v = priceY(1:10)   % get elements from 1 to 10

save hello.mat v    % save data from variable v to the file named hello.mat

save filename variable -ascii (or -text et cetera)

A(2,3)    
A(2,:)    % ':' refer to a range
A(m)    % one parameter, m can be a number or a range
A(m, n)    % two params, choose elements by row and column


Computing on data

C = A * B   

% element wise functions
A .* B    % element-wise multiplications of two matrices
A .^ 2
1 ./ A
log(v)
exp(v)
abs(v)
-v   % minus v

A'   % the transpose of A

[value, index] = max(a)   % when a is a matrix, max() will be executed in every column

find(a < 3)    % find all the elements that are less than 3

magic(m)     % m by m matrix that complies with sudoku

[r, c] = find(A >= 7)    % row r, column c
i = find(A >= 7)   % a column vector of indexes

sum(a)
prod(a)
floor(a)
ceil(a)

max(A, B, 1(or 2))    % matrix A and B, 1: get by columns, 2: get by rows

max(A(:))
max(max(A))

sum(A, 1(or 2))   % sum by columns(1), or rows(2)

sum(sum(A .* eye(9)))    % sum of the diagonal elements

flipud()    % flip updown matrix

pinv(A)    % the inverse of A

Plotting data

t = [0:0.01:0.98];
y1 = sin(2*pi*t);
plot(t, y1);
hold on;
plot(t, y2);
xlabel('time');
ylabel('value');
legend('sin', 'cos')
title('my plot')
axis([0 4 -1 10])   % set axis range
print -dpng 'myplot.png'    % save image
close    % close current figure


figure(1); plot(t, y1)
figure(2); plot(t, y2)
subplot(1,2,1);
clf    % clear current figure

imagesc(magic(15))
colorbar
colormap gray    % background image

Control statements

% if

c = 1;
if c == 1,
  disp('The value is one');
elseif c == 2,
  disp('The value is two');
else
  disp('We don\'t know the value')
end   % end if



% for 

for i = 1:10,
  disp(i);
end;



% while

i = 1;
while i<5,
  disp(i);
  ++i;
end;

Function

create a file named by ***.m in the current path
or you have to addpath(‘absolute/relative path’)

    原文作者:stanf1l
    原文地址: https://www.jianshu.com/p/ab34b623f8f1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞