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
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值