Fork me on GitHub

Matlab基础

命令行基础

x = [1,2,3]
x = 1:3
A = [1,2;3,4;5,6]
B = ones(3,4)
C = eye(4)              #单位矩阵
D = zeros(3,4)          #零矩阵
det(C)                  #行列式
x = inv(A)*b'           #解线性方程,等价于A^-1*b'
e = eig(A)              #返回一个列向量,其中包含方阵 A 的特征值。
[V,D] = eig(A)          #返回特征值的对角矩阵 D 和矩阵 V,其列是对应的右特征向量,使得 A*V = V*D

norm(x)                 #计算向量范数和矩阵范数
gallery()               #生成测试矩阵

dot(x,y)                #向量点乘
cross(x,y)              #向量叉乘

A.*B                    #矩阵点乘(对应元素相同,要求两个矩阵大小相同)
A*B                     #矩阵叉乘(要求A的列数等于B的行数whos                    #查看当前工作空间
class()                 #数据类型

函数

function y = sinh( x )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
y = (exp(x) - exp(-x))/2;
end

循环

>> for i = [1 3 5 7 9]
disp(i);
end
>> E = randn(1000,1);
>> SSE = 0;
>> for i = 1:1000
SSE = SSE + E(i)*E(i);
end
>> SSE

SSE =

  983.5102

>> SSE/1000

ans =

    0.9835
#对一段程序计时
tic
E = randn(1000,1);
SSE = 0;
for i = 1:1000
SSE = SSE + E(i)*E(i);
end
MSE =  SSE/1000;
toc
#Elapsed time is 0.016618 seconds.

# 和点乘比较速度,明显点乘较快
tic
E = randn(1000,1);
MSE = E.*E /1000;
toc
# Elapsed time is 0.010042 seconds.

选择

x=1:10;
y=zeros(1,10);
for i =1:10
    if mod(x(i),2) == 0
        y(i) = 1;
    else
        y(i) = 0;
    end
end

#另一种写法,Sum输出为18
X = 1:10;           
Sum = 0;
for x = X           #x会遍历X中的元素
    if mod(x,3) == 0
        Sum =Sum +x;
    end
end
x = 1:10
found = 0;
i = 0;
while ~found
    i = i +1
    if x(i) == 8
        disp('I found it');
        found = 1;
    end
end

# or
for i =1:10
    fprintf('i = %d\n',i);
    if x(i) ==8
        disp('i found it');
        break;
     end
 end

数据结构

结构体

>> my_struct.name='niuhe'

my_struct = 

    name: 'niuhe'

>> class(my_struct)

ans =

struct

>> my_struct.age = 25

my_struct = 

    name: 'niuhe'
     age: 25

>> class(my_struct.name)

ans =

char

>> isfield(my_struct,'name')

ans =

     1

>> isfield(my_struct,'gender')

ans =

     0

>> rmfield(my_struct,'age')

ans = 

    name: 'niuhe'

>> setfield(my_struct,'gender','f')

ans = 

      name: 'niuhe'
       age: 25
    gender: 'f'

>> S = struct('name','bob','age',32,'email','123@gmail.com')

S = 

     name: 'bob'
      age: 32
    email: '123@gmail.com'

哈希表

>> my_cell{1} = 'hello world.'

my_cell = 

    'hello world.'

>> my_cell{'A'} = [1,2,3,4,5]

my_cell = 

  Columns 1 through 9

    'hello world.'    []    []    []    []    []    []    []    []

  Columns 10 through 20

    []    []    []    []    []    []    []    []    []    []    []

  Columns 21 through 31

    []    []    []    []    []    []    []    []    []    []    []

  Columns 32 through 42

    []    []    []    []    []    []    []    []    []    []    []

  Columns 43 through 53

    []    []    []    []    []    []    []    []    []    []    []

  Columns 54 through 64

    []    []    []    []    []    []    []    []    []    []    []

  Column 65

    [1x5 double]

>> my_cell{1}

ans =

hello world.

>> my_cell{'A'}

ans =

     1     2     3     4     5

plot画图

>> x = [0 0.1 0.2 0.3 ];
>> y = 1:4;
>> plot(x,y);

>> x = linspace(0,100,200);
>> y =sin(x);
>> plot(x,y);

>> x = linspace(0,2*pi,100);
>> y1 = sin(x);
>> y2 = cos(x);
>> plot(x,y1,x,y2);
>> plot(x,y1,'-',x,y2,'.');
>> plot(x,y1,'--',x,y2,'.');

>> bar(x)               #画柱状图

>> x = randn(1000,1)    #生成1000*1的随机数矩阵,服从均值是0,方差是1的正太分布    
>> hist(x)
>> hist(x,50)

>> x = 1:5;
>> pie(x);

>> x = linspace(0,2*pi,1000);
>> y = 10*sin(x) + randn(1,1000);
>> plot(x,y);
>> scatter(x,y);
>> x = randn(1000,1) * 2;
>> y = 5*sin(x) + rand(1000,1);
>> plot(x,y);
>> scatter(x,y);
subplot()           #子图

surf()
contour()

title()
xlabel()
ylabel()

读写文件

CSV文件

csvread()
csvwrite()

# 保存现有工作空间
save()
load()

Comments