天天看點

Matlab—matlab進行資料處理使用Matlab進行資料處理

使用Matlab進行資料處理

一、 一維數組建立:

(1)直接輸入法:

test=[1 2 3 4]

test=[1;2;3;4]

>> test = [2 4 6 8]

test =
 2     4     6     8

>> test = [2;4;6;8]

test =

 2
 4
 6
 8
           

(2)步長生成法:

test=1:0.5:10

>> test=1:0.5:10

test =

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000    5.5000    6.0000    6.5000    7.0000    7.5000    8.0000    8.5000    9.0000    9.5000   10.0000
           

(3)定數線性采樣法:

test = linspace(1,12,5)

>> test = linspace(1,12,5)

test =

    1.0000    3.7500    6.5000    9.2500   12.0000
           

(4)定數對數采樣法:

logspace(2,6,4)

>> logspace(2,6,4)

ans =
    1.0e+006 *

    0.0001    0.0022    0.0464    1.0000
           

二、 高維數組建立實驗:

(1)直接輸入法:

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

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

A =

    1     2     3
    4     5     6
    7     8     9
           

(2)使用下标:

clear,A(2,3,2)=1

>> clear
>> A(2,3,2)=1

A(:,:,1) =

    0     0     0
    0     0     0


A(:,:,2) =

    0     0     0
    0     0     1
           

(3)使用低維數組:

clear,A=eye(3,4);A(:,:,2)=eye(3,4)*2;A(:,:,3)=eye(3,4)*3;A(:,:,4)=eye(3,4)*4

>> clear
>> A=eye(3,4);
>> A(:,:,2)=eye(3,4)*2;
>> A(:,:,2)=eye(3,4)*2;
>> A(:,:,4)=eye(3,4)*4

A(:,:,1) =

    1     0     0     0
    0     1     0     0
    0     0     1     0


A(:,:,2) =

    2     0     0     0
    0     2     0     0
    0     0     2     0


A(:,:,3) =

    3     0     0     0
    0     3     0     0
    0     0     3     0


A(:,:,4) =

    4     0     0     0
    0     4     0     0
    0     0     4     0
           

(4)建立函數(cat、repmat、reshape)建立高維數組:

cat(3,[1,2,3;4,5,6],eye(2,3)*2,ones(2,3))

>> cat(3,[1,2,3;4,5,6],eye(2,3)*2,ones(2,3))

ans(:,:,1) =

    1     2     3
    4     5     6


ans(:,:,2) =

    2     0     0
    0     2     0


ans(:,:,3) =

    1     1     1
    1     1     1
           

repmat([1,2;3,4],[1,2,3])

>> repmat([1,2;3,4],[1,2,3])

ans(:,:,1) =

    1     2     1     2
    3     4     3     4


ans(:,:,2) =

    1     2     1     2
    3     4     3     4


ans(:,:,3) =

    1     2     1     2
    3     4     3     4
           

reshape(1:20,2,5,2)

>> reshape(1:20,2,5,2)

ans(:,:,1) =

    1     3     5     7     9
    2     4     6     8    10


ans(:,:,2) =

    11    13    15    17    19
    12    14    16    18    20
           

三、标準數組建立實驗:

(1)全0矩陣:>> zeros(3)

>> zeros(3)

ans =

    0     0     0
    0     0     0
    0     0     0

>> zeros(5)

ans =

    0     0     0     0     0
    0     0     0     0     0
    0     0     0     0     0
    0     0     0     0     0
    0     0     0     0     0
           

(2)全1矩陣:>> ones(5)

>> ones(5)

ans =

    1     1     1     1     1
    1     1     1     1     1
    1     1     1     1     1
    1     1     1     1     1
    1     1     1     1     1
           

(3)機關矩陣:>> eye(4)

>> eye(4)

ans =

    1     0     0     0
    0     1     0     0
    0     0     1     0
    0     0     0     1
           

(4)magic矩陣:>> magic(4)

>> magic(4)

ans =

    16     2     3    13
    5    11    10     8
    9     7     6    12
    4    14    15     1

>> magic(3)

ans =

    8     1     6
    3     5     7
    4     9     2
           

(5)随機矩陣:>> randn(4)

>> randn(4)

ans =

    0.5377    0.3188    3.5784    0.7254
    1.8339   -1.3077    2.7694   -0.0631
    -2.2588   -0.4336   -1.3499    0.7147
    0.8622    0.3426    3.0349   -0.2050

>> randn(3)

ans =

    -0.1241    1.4172    0.7172
    1.4897    0.6715    1.6302
    1.4090   -1.2075    0.4889
           

四、矩陣變換實驗:

令 Data = [1,2,3;4,5,6;7,8,9],分别使用diag、’、fliplr、flipud、rot90、tril、triu函數計算Data的對角、轉置、翻轉、旋轉、三角矩陣,具體指令如下:

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

diag(Data)

(Data)’

fliplr(Data)

flipud(Data)

rot90(Data)

tril(Data)

triu(Data)

>> Data = [1,2,3;4,5,6;7,8,9]

Data =

    1     2     3
    4     5     6
    7     8     9

>> diag(Data)

ans =

    1
    5
    9

>> (Data)'

ans =

    1     4     7
    2     5     8
    3     6     9

>> fliplr(Data)

ans =

    3     2     1
    6     5     4
    9     8     7

>> flipud(Data)

ans =

    7     8     9
    4     5     6
    1     2     3

>> rot90(Data)

ans =

    3     6     9
    2     5     8
    1     4     7

>> tril(Data)

ans =

    1     0     0
    4     5     0
    7     8     9

>> triu(Data)

ans =

    1     2     3
    0     5     6
    0     0     9
           

五、字元串數組建立與操作實驗:

(1)建立字元串數組:

arr=str2mat(‘I’,’want’,’to’,’study’,’matlab’)

>> arr=str2mat('I','want','to','study','matlab')

arr =

I     
want  
to    
study 
matlab
           

(2)去掉字元串末尾的空格deblank:

建立字元串,用abs函數驗證空格的存在;用deblank去掉空格,用abs已經去掉空格

x=’M a t l a b ‘;y=abs(x)

z=deblank(x);w=abs(z)

>> x='M a t l a b  ';
>> x

x =

M a t l a b  

>> y=abs(x)

y =

    77    32    97    32   116    32   108    32    97    32    98    32    32

>> z=deblank(x)

z =

M a t l a b

>> a=abs(z) 

a =

    77    32    97    32   116    32   108    32    97    32    98
           

(3) 删除字元串開頭和結尾的空格strtrim

x=’ M a t l a b ‘;

x=strtrim(str1)

>> x='  M a t l a b  ';
>> abs(x)

ans =

    32    32    77    32    97    32   116    32   108    32    97    32    98    32    32

>> x = strtrim(x)

x =

M a t l a b

>> abs(x)

ans =

    77    32    97    32   116    32   108    32    97    32    98
           

(4) 執行簡單的字元串替代strrep、

str1=’I want to study Matlab.’;

str2=’Matlab’;

str3=’Matlab too’;

str=strrep(str1,str2,str3)

>> str1='I want to study Matlab.';
>> str2='Matlab';
>> str3='matlab too';
>> str=strrep(str1,str2,str3)

str =

    I want to study matlab too.
           

(5)規範格式strread;

strread(‘0.231’,’%5.3f’)

>> strread('0.231','%5.3f')

ans =

    0.2310
           

(6) 函數strtok找出由特定字元指定的字元串内的标記;

str = ‘I want to study Matlab’

strtok(str,’t’)

>> str = 'I want to study Matlab'

str =

    I want to study Matlab

>> strtok(str, 't')

ans =

    I wan
           

六、 架構數組的建立與操作實驗:

(1)直接建立法:

clear m; m.real = [1 2 3 4 5]; m.imag = ones(4)

>> clear m;
>> m.real = [1 2 3 4 5];
>> m.imag = ones(4)

m = 

    real: [1 2 3 4 5]
    imag: [4x4 double]
           

(2)指令(struct)建立法

s = struct(‘name’,{‘x’,’y’},’id’,{‘3’,’4’},’w’,{3,4})

>> s = struct('name',{'x','y'},'id',{'3','4'},'w',{3,4})

s = 

1x2 struct array with fields:
    name
    id
    w
           

(3)Fieldnames函數:

fieldnames(s)

>> fieldnames(s)

ans = 

    'name'
    'id'
    'w'
           

(4)Getfield函數:

str(1,1).name = ‘x’;

str(1,1).ID = 5;

str(2,1).name = ‘y’;

str(2,1).ID = 3;

result = getfield(str, {2,1}, ‘name’)

>> clear
>> str(1,1).name = 'x';
>> str(1,1).ID = 5;
>> str(2,1).name = 'y';
>> str(2,1).ID = 3;
>> result = getfield(str, {2,1}, 'name')

result =

    y

>> result = getfield(str, {1,1}, 'name')

result =

    x
           

(5)Setfield函數:

str(1,1).name = ‘x’;

str(1,1).ID = 5;

str(2,1).name = ‘y’;

str(2,1).ID = 3;

str= setfield(str,{2,1},’name’,’a’);

str(2,1).name

>> str(1,1).name = 'x';
>> str(1,1).ID = 5;
>> str(2,1).name = 'y';
>> str(2,1).ID = 3;
>> str= setfield(str,{2,1},'name','a');
>> str(2,1).name

ans =

    a
           

七、 基本運算符号實驗:

(1)矩陣加:

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

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

a+b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
>> a+b

ans =

    10    10    10
    10    10    10
    10    10    10
           

(2)矩陣減:

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

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

a-b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
>> a-b

ans =

    -8    -6    -4
    -2     0     2
    4     6     8
           

(3)矩陣乘

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

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

a*b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
>> a*b

ans =

    30    24    18
    84    69    54
    138   114    90
           

(4)數組乘

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

b=[3,6,9;1,2,3;2,4,6];

a.*b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
ans =

    9    16    21
    24    25    24
    21    16     9
           

(5)矩陣乘方

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

a^2

>> a=[1,2,3;4,5,6;7,8,9];
ans =

    30    36    42
    66    81    96
    102   126   150
           

(6)數組乘方

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

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

a.^b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
>> a.^b

ans =

       1         256        2187
    4096        3125        1296
     343          64           9
           

(7)矩陣左除

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

b=[2;4;6];

a\b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[2;4;6];   
>> a\b
Warning: Matrix is close to singular or badly scaled.
     Results may be inaccurate. RCOND = 1.541976e-018. 

ans =

    -0.6667
    1.3333
    0
           

(8)矩陣右除

a=ones(3);

b=[1,1,1];

a/b

>> x = ones(3)

x =

    1     1     1
    1     1     1
    1     1     1


>> y = [1,1,1]

y =

    1     1     1

>> x/y

ans =

    1
    1
    1
           

(9)數組左除

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

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

a.\b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];

>> a.\b

ans =

    9.0000    4.0000    2.3333
    1.5000    1.0000    0.6667
    0.4286    0.2500    0.1111
           

(10)數組右除

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

b=[3,6,9;1,2,3;2,4,6];

a./b

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[9,8,7;6,5,4;3,2,1];
>> a./b

ans =

    0.1111    0.2500    0.4286
    0.6667    1.0000    1.5000
    2.3333    4.0000    9.0000
           

(11)克羅内克張量積

a=[1,0,1;1,1,1;1,0,1];

b=[0,0,1;1,0,1;0,0,1];

kron(a,b)

>> a=[1,0,1;1,1,1;1,0,1];
>> b=[0,0,1;1,0,1;0,0,1];
>> kron(a,b)

ans =

    0     0     1     0     0     0     0     0     1
    1     0     1     0     0     0     1     0     1
    0     0     1     0     0     0     0     0     1
    0     0     1     0     0     1     0     0     1
    1     0     1     1     0     1     1     0     1
    0     0     1     0     0     1     0     0     1
    0     0     1     0     0     0     0     0     1
    1     0     1     0     0     0     1     0     1
    0     0     1     0     0     0     0     0     1
           

(12)邏輯與

a=[1,0,1;1,1,1;1,0,1];

b=[0,0,1;1,0,1;0,0,1];

a&b

>> a=[1,0,1;1,1,1;1,0,1];
>> b=[0,0,1;1,0,1;0,0,1];
>> a&b

ans =

    0     0     1
    1     0     1
    0     0     1
           

(13)邏輯或

a=[1,0,1;1,1,1;1,0,1];

b=[0,0,1;1,0,1;0,0,1];

a|b

>> a=[1,0,1;1,1,1;1,0,1];
>> b=[0,0,1;1,0,1;0,0,1];
>> a|b

ans =

    1     0     1
    1     1     1
    1     0     1
           

(14)邏輯非

a=[1,0,1;1,1,1;1,0,1];

~a

>> ~a

ans =

    0     1     0
    0     0     0
    0     1     0
           

(15)邏輯異或

a=[1,0,1;1,1,1;1,0,1];

b=[0,0,1;1,0,1;0,0,1];

xor(a,b)

>> xor(a,b)

ans =

    1     0     0
    0     1     0
    1     0     0
           

八、 矩陣分析實驗:

(1)範數(norm):

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

norm(a,1)

norm(a,2)

>> a=[1,2,3;4,5,6;7,8,9];
>> norm(a,1)

ans =

    18

>> norm(a,2)

ans =

    16.8481 
           

(2)條件數(cond):

cond(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> cond(a)

ans =

    3.8131e+016
           

(3)行列式(det):

det(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> det(a)

ans =

    6.6613e-016
           

(4)秩(rank):

rank(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> rank(a)

ans =

    2
           

(5)特征值(eig):

eig(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> eig(a)

ans =
    16.1168
    -1.1168
    -0.0000
           

[V,D]=eig(a)

>> [V,D]=eig(a)

V =

    -0.2320   -0.7858    0.4082
    -0.5253   -0.0868   -0.8165
    -0.8187    0.6123    0.4082
D =

    16.1168         0         0
     0   -1.1168         0
     0         0   -0.0000
           

(6)化零矩陣(null)

Z=null(a)

>> a

a =

    1     2     3
    4     5     6
    7     8     9

>> Z=null(a)

Z =
    -0.4082
     0.8165
    -0.4082
           

(7)Cholesky分解(chol)

a=pascal(3);

chol(a)

>> a=pascal(3)

a =

    1     1     1
    1     2     3
    1     3     6

>> chol(a)

ans =

    1     1     1
    0     1     2
    0     0     1
           

(8)LU分解(lu)

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

[L1,U1]=lu(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> [L1,U1]=lu(a)

L1 =

    0.1429    1.0000         0
    0.5714    0.5000    1.0000
    1.0000         0         0


U1 =

    7.0000    8.0000    9.0000
     0    0.8571    1.7143
     0         0    0.0000
           

(9)正交分解(qr)

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

[U,S]=qr(a)

>> [U,S]=qr(a)

U =

    0.1231    0.9045    0.4082
    0.4924    0.3015   -0.8165
    0.8616   -0.3015    0.4082


S =

    8.1240    9.6011   11.0782
     0    0.9045    1.8091
     0         0    0.0000
           

(10)奇異值分解(svd):

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

[U,S,V]=svd(a)

>> [U,S,V] = svd(a)

U =

    -0.2148    0.8872    0.4082
    -0.5206    0.2496   -0.8165
    -0.8263   -0.3879    0.4082


S =

    16.8481         0         0
     0    1.0684         0
     0         0    0.0000


V =

    -0.4797   -0.7767   -0.4082
    -0.5724   -0.0757    0.8165
    -0.6651    0.6253   -0.4082
           

九、 數值計算實驗:(在操作時提示有警告資訊,說此方法以經删除)

(1) 導數(diff):

a=’5*x^2+8*x+10’

diff(a)

>> a = '5*x^2+8*x+10'

a =

5*x^2+8*x+10

>> diff(a)
Warning: The method char/diff will be removed in a future release. Use sym/diff instead. For examplediff(sym('x^2')). After removal diff('x^2') will return diff(double('x^2')). 
> In char.diff at 10

ans =

    10*x + 8
           

可以使用另一種方法進行操作:

>> diff(sym('5*x^2+8*x+10'))

ans =

    10*x + 8
           

(2)梯度(gradient)

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

[fx,fy]=gradient(a)

>> a=[1,2,3;4,5,6;7,8,9];
>> [fx,fy]=gradient(a)

fx =

    1     1     1
    1     1     1
    1     1     1


fy =

    3     3     3
    3     3     3
    3     3     3
           

(3)多項式求根(roots)、

p=[1,3,2,5];

px=poly2str(p,’x’);

r=roots(p)

>> p=[1,3,2,5];
>> px=poly2str(p,'x');
>> r = roots(p)

r =
    -2.9042
    -0.0479 + 1.3112i
    -0.0479 - 1.3112i
           

(4)零點(fzero、fsolve):

a=@(x)x^2+3*x+2;

x=fzero(a,0)

x=fsolve(‘x^2+3*x+2’,0)

>> a=@(x)x^2+3*x+2;
>> x=fzero(a,0)

x =

    -1

>> x=fsolve('x^2+3*x+2',0)

Optimization terminated: first-order optimality is less than options.TolFun.

x =

  -1
           

(5)極值(fminbnd、fminsearch、fminunc)、

f=@(x) x^2-4*x+5;

fminbnd(f,0,1)

>> f=@(x) x^2-4*x+5;
>> fminbnd(f,0,1)

ans =

    0.9999
           

fun=inline(‘x(1)^2-3*x(1)*x(2)+2*x(2)^2’);

x0=[1,1];

fminsearch(fun,x0)

>> fun=inline('x(1)^2-3*x(1)*x(2)+2*x(2)^2');
>> x0=[1,1];
>> fminsearch(fun,x0)

Exiting: Maximum number of function evaluations has been exceeded
    - increase MaxFunEvals option.
    Current function value: -448408571070688420000000000000000000000000000000000000000000000000000000000000000000.000000 


ans =

    1.0e+042 *

        1.8946    1.4090
           

fun=inline(‘x(1)^2-3*x(1)*x(2)+2*x(2)^2’);

x0=[1,1];

fminunc(fun,x0)

>> fminunc(fun,x0)
Warning: Gradient must be provided for trust-region algorithm;
using line-search algorithm instead. 
> In fminunc at 347

Solver stopped prematurely.

fminunc stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 200 (the default value).


ans =

    1.0e+005 *

        7.5541    5.3958
           

(6)積分(quadl)

用内聯函數定義被積函數:

fun=inline(‘-x.*x’,’x’);

y=quadl(fun,0,1)

>> fun=inline('-x.*x','x');
>> y=quadl(fun,0,1)

y =

    -0.3333
           

十、 符号計算實驗:

(1)先用syms定義符号變量,再用simplify函數進行化簡,具體指令如下:

simplify(cos(x)+sqrt(-sin(x)^2))

>> syms x y z;
>> simplify(cos(x)+sqrt(-sin(x)^2))

ans =

    cos(x) + (-sin(x)^2)^(1/2)
           

(2)用solve函數求解,指令如下:

x=solve(‘(x+2)^x=16’,’x’)

>> x=solve('(x+2)^x=16','x')

    x =

        2.0
           

繼續閱讀