Octave(MATLAB)

Octave笔记

    • 变量
    • 矩阵操作
    • 常用函数
    • 决策
    • 绘图

MATLAB w3c

MATLAB_台大郭彦甫

变量

变量赋值

a = 1;
A = 2;
a
A
a =  1
A =  2

数值型

class(int8(1))
class([1 2 3])
ans = int8
ans = double

字符串

class('Hello World!')
ans = char

逻辑型

class(true)
ans = logical

结构体

student.name = 'John Doe';
student.id = 301073268;
student.grade = [100 75 73;...
                 95 91 85;...
                 100 98 72];
class(student)
student
ans = struct
student =

  scalar structure containing the fields:

    name = John Doe
    id =  301073268
    grade =

       100    75    73
        95    91    85
       100    98    72

元胞

A{1, 1} = [1 4 3; 0 5 8; 7 2 9];
A{1, 2} = 'Anne Smith';
A{2, 1} = 3 + 7i;
A{2, 2} = -pi:pi:pi;
class(A)
A
error: scalar cannot be indexed with {
error: scalar cannot be indexed with {
error: scalar cannot be indexed with {
error: scalar cannot be indexed with {
ans = double
A =  2

矩阵操作

创建向量

vr = [1 2 3]    % 行向量
vc = [1; 2; 3]  % 列向量
vr =

   1   2   3

vc =

   1
   2
   3

创建矩阵

M = [1 2 3; 4 5 6; 7 8 9]
M =

   1   2   3
   4   5   6
   7   8   9

矩阵索引

M = [1 2 3; 4 5 6; 7 8 9]
M(8)           % 索引从1开始,第8个元素
M(:, 2)        % 第2列
M(1:2, [1 3])  % 1,2行1,3列
M =

   1   2   3
   4   5   6
   7   8   9

ans =  6
ans =

   2
   5
   8

ans =

   1   3
   4   6

特殊矩阵

a = linspace(1, 10, 3)  % 间隔数组
b = eye(2)              % 单位矩阵
c = zeros(2)            % 零矩阵
d = ones(2)             % 一矩阵
e = diag([1 2 3])       % 对角矩阵
f = rand(2)             % U(0,1)
g = randn(2)            % N(0,1)
a =

    1.0000    5.5000   10.0000

b =

Diagonal Matrix

   1   0
   0   1

c =

   0   0
   0   0

d =

   1   1
   1   1

e =

Diagonal Matrix

   1   0   0
   0   2   0
   0   0   3

f =

   0.54306   0.91400
   0.27150   0.13563

g =

   0.31237  -1.02912
  -0.42424  -0.58838

矩阵运算

M1 = magic(3);
M2 = inv(M1);

M1 * M2   % 矩阵乘法
M1 .* M2  % 矩阵点乘
ans =

   1.00000   0.00000  -0.00000
  -0.00000   1.00000   0.00000
   0.00000   0.00000   1.00000

ans =

   1.177778  -0.144444   0.383333
  -0.183333   0.111111   0.738889
  -0.077778   1.700000  -0.205556

矩阵与向量间运算

v = [1; 2; 3]
M = ones(3)
M + v
v =

   1
   2
   3

M =

   1   1   1
   1   1   1
   1   1   1

ans =

   2   2   2
   3   3   3
   4   4   4

矩阵合并

M1 = magic(3);
M2 = M1';

M3 = [M1, M2]  % 水平合并
M4 = [M1; M2]  % 竖直合并
M3 =

   8   1   6   8   3   4
   3   5   7   1   5   9
   4   9   2   6   7   2

M4 =

   8   1   6
   3   5   7
   4   9   2
   8   3   4
   1   5   9
   6   7   2

***操作符

a = 1:3
b = 1:2:5
c = 9:-1:7
a =

   1   2   3

b =

   1   3   5

c =

   9   8   7

常用函数

自定义函数  f u n c t i o n   [ o u t 1 , o u t 2 , . . . , o u t M ]   =   f u n N a m e ( i n 1 , i n 2 , i n 3 , . . . , i n N ) function [out1,out2, ..., outM] = funName(in1,in2,in3, ..., inN) function [out1,out2,...,outM] = funName(in1,in2,in3,...,inN)

最大值、最小值

M = magic(3)
max(M)   % 最大值
min(M)   % 最小值
M =

   8   1   6
   3   5   7
   4   9   2

ans =

   8   9   7

ans =

   3   1   2

平均值、标准差

M = magic(3)
mean(M)  % 平均值
std(M)   % 标准差
M =

   8   1   6
   3   5   7
   4   9   2

ans =

   5   5   5

ans =

   2.6458   4.0000   2.6458

求和

M = magic(3)
sum(M)   % 求和
M =

   8   1   6
   3   5   7
   4   9   2

ans =

   15   15   15

排序

M = magic(3)
sort(M)  % 排序
M =

   8   1   6
   3   5   7
   4   9   2

ans =

   3   1   2
   4   5   6
   8   9   7

维度

M = ones(2, 3)
size(M)
M =

   1   1   1
   1   1   1

ans =

   2   3

转置、逆、行列式、秩、最简形

M = magic(3)
A = M'
B = inv(M)
C = det(M)
D = rank(M)
E = rref(M)
M =

   8   1   6
   3   5   7
   4   9   2

A =

   8   3   4
   1   5   9
   6   7   2

B =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

C = -360
D =  3
E =

   1   0   0
   0   1   0
   0   0   1

长度

s = 'Hello World!'
length(s)
v = [1 2 3]
length(v)
M = zeros(2, 3)
length(M)
s = Hello World!
ans =  12
v =

   1   2   3

ans =  3
M =

   0   0   0
   0   0   0

ans =  3

决策

逻辑判断

1 == 2
1 ~= 2
1 < 2
true && false
true || false
~false
ans = 0
ans = 1
ans = 1
ans = 0
ans = 1
ans = 1

if 语句

a = 30;
if a == 10
    disp('Value of a is 10');
elseif a == 20 || a == 30
    disp('Value of a is 20 or 30');
else
    disp('None of the values are matching');
end
Value of a is 20 or 30

switch 语句

grade = 'B';
switch(grade)
    case 'A'
        disp('Excellent!');
    case 'B'
        disp('Well done');
    case 'C'
        disp('Well done');
    case 'D'
        disp('You passed');
    case 'F'
        disp('Better try again');
    otherwise
        disp('Invalid grade');
end
Well done

while 语句

a = 10;
while a < 20
    fprintf('value of a: %d \n', a);
    a = a + 1;
end
value of a: 10 
value of a: 11 
value of a: 12 
value of a: 13 
value of a: 14 
value of a: 15 
value of a: 16 
value of a: 17 
value of a: 18 
value of a: 19 

for 语句

for a = 10:20
    fprintf('value of a: %d \n', a);
end
value of a: 10 
value of a: 11 
value of a: 12 
value of a: 13 
value of a: 14 
value of a: 15 
value of a: 16 
value of a: 17 
value of a: 18 
value of a: 19 
value of a: 20 

绘图

x = linspace(-pi, pi, 100);
y1 = sin(x);
y2 = cos(x);
hold on                     % 一张图中显示多条线
plot(x, y1, 'r');
plot(x, y2, 'b');
legend('sin(x)', 'cos(x)')  % 标签
title('Graph')              % 标题
xlabel('x')                 % 横坐标名称
ylabel('y')                 % 纵坐标名称
axis([-pi pi -1 1])         % 坐标轴范围
hold off

Octave(MATLAB)_第1张图片

你可能感兴趣的:(Octave(MATLAB))