MATLAB学习



========================BasicOperations========================

>> A=rand(3,2)

 

A =

 

   0.8147    0.9134

   0.9058    0.6324

   0.1270    0.0975

 

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

 

A =

 

    1     2

    3     4

    5     6

 

>> A(3,2)

 

ans =

 

    6

 

>> A(2,:)

 

ans =

 

    3     4

 

>> A([1 3],:) %select the 1st and 3rd row

 

ans =

 

    1     2

    5     6

 

>> save test.mat A

>> save testtext.txt A –ascii

 



>> A(:,2)=[10,11,12]

 

A =

 

    1    10

    3    11

    5    12

 

>> A=[A,[101;102;103]]%append another column vector

 

A =

 

    1    10   101

    3    11   102

    5    12   103

 

>> A(:)%put all elements of A into a single vector

 

ans =

 

    1

    3

    5

   10

   11

   12

  101

  102

  103

 

B=[11,12;13,14;15,16]

 

B =

 

   11    12

   13    14

   15    16

 

>> C=[A B]

 

C =

 

    1    10   101   11    12

    3    11   102   13    14

    5    12   103   15    16

 

A=[1 2;3 4;5 6]

 

A =

 

    1     2

    3     4

    5     6

 

>> C=[A;B]

 

C =

 

    1     2

    3     4

    5     6

   11    12

   13    14

15    16

 

========================Computingon Data========================

 

>>A.*B%对应位置的数据相乘,element product

 

ans =

 

   11    24

   39    56

75    96

 

>>A'%转置

 

ans =

 

    1     3     5

    2     4     6

 

>> A<3

 

ans =

 

    1     1

    0     0

    0     0

 

>> find(A<3)

 

ans =

 

    1

    4

 

A =

 

    8     1     6

    3     5     7

    4     9     2

 

>> [r,c]=find(A>=6)

 

r =

 

    1

    3

    1

    2

 

 

c =

 

    1

    2

    3

    3

 

a=[1 15 2 0.5]

 

a =

 

   1.0000   15.0000    2.0000   0.5000

 

>> sum(a)

 

ans =

 

  18.5000

 

>> prod(a)

 

ans =

 

   15

 

>> floor(a)%取下界

 

ans =

 

    1    15     2    0

 

>> ceil(a)%取上界

 

ans =

 

    1    15     2    1

 

rand(3)%创建3*3random矩阵,每个值在[0,1]之间

 

ans =

 

   0.6463    0.2760    0.1626

   0.7094    0.6797    0.1190

   0.7547    0.6551    0.4984

 

>> max(rand(3),rand(3)) %在两个random3*3矩阵中找对应位置的max

 

ans =

 

   0.9597    0.2238    0.5060

   0.5472    0.7513    0.8143

0.5853    0.8407   0.8909

 

A=magic(3)

 

A =

 

    8     1     6

    3     5     7

    4     9     2

 

>> max(A,[],1) %找每列最大值,1表示第一维,即列

 

ans =

 

    8     9     7

 

>> max(A,[],2) %找每行最大值,2表示第二维,即行

 

ans =

 

    8

    7

    9

 

max(A) %defaultis column max

 

ans =

 

    8     9     7

 

>> max(max(A))

 

ans =

 

    9

 

>> A(:)

 

ans =

 

    8

    3

    4

    1

    5

    9

    6

    7

    2

 

>> max(A(:))

 

ans =

 

    9

 

>>

>>

>>

>> A=magic(9)

 

A =

 

   47    58    69   80     1    12   23    34    45

   57    68    79    9    11    22   33    44    46

   67    78     8   10    21    32   43    54    56

   77     7    18   20    31    42   53    55    66

    6    17    19   30    41    52   63    65    76

   16    27    29   40    51    62   64    75     5

   26    28    39   50    61    72   74     4    15

   36    38    49   60    71    73    3    14    25

   37    48    59   70    81     2   13    24    35

 

>> sum(A,1)%columnsum

 

ans =

 

  369   369   369  369   369   369  369   369   369

 

>> sum(A,2)%sumeach row

 

ans =

 

  369

  369

  369

  369

  369

  369

  369

  369

  369

 

>> eye(9)

 

ans =

 

    1     0     0    0     0     0    0     0     0

    0     1     0    0     0     0    0     0     0

    0     0     1    0     0     0    0     0     0

    0     0     0    1     0     0    0     0     0

    0     0     0    0     1     0    0     0     0

    0     0     0    0     0     1    0     0     0

    0     0     0    0     0     0    1     0     0

    0     0     0    0     0     0    0     1     0

    0     0     0    0     0     0    0     0     1

 

>> A.*eye(9)%takethe element product of the 2 matrix

 

ans =

 

   47     0     0    0     0     0    0     0     0

    0    68     0    0     0     0    0     0     0

    0     0     8    0     0     0    0     0     0

    0     0    0    20     0    0     0     0    0

    0     0     0    0    41     0    0     0     0

    0     0     0    0     0    62    0     0     0

    0     0     0    0     0     0   74     0     0

    0     0     0    0     0     0    0    14     0

    0     0     0    0     0     0    0     0    35

 

>> sum(sum(A.*eye(9)))

 

ans =

 

  369

 

>> A=magic(3)

 

A =

 

    8     1     6

    3     5     7

    4     9     2

 

>> temp=pinv(A) %矩阵求逆

 

temp =

 

   0.1472   -0.1444    0.0639

  -0.0611    0.0222    0.1056

  -0.0194    0.1889   -0.1028

 

>> temp*A

 

ans =

 

   1.0000   -0.0000   -0.0000

  -0.0000    1.0000    0.0000

   0.0000    0.0000    1.0000

 

========================PlottingData========================

 

>>t=[0:0.01:0.98];

>> y1=sin(2*pi*4*t);

>> plot(t,y1)


>> hold on;%plotnew figure on the old ones

>> y2=cos(2*pi*4*t);

>> plot(t,y2,'r')

 

>> xlabel('time')
>> ylabel('value')
>> legend('sin','cos')
>> title('my plot')
>> print -dpng 'myplot.png'%save as a file in default catalog

>> close



%分别显示两幅图像

>> figure(1);plot(t,y1);

>> figure(2);plot(t,y2)
>> 

%一幅图中显示两个subplot

 figure
subplot(1,2,1);plot(t,y1)
subplot(1,2,2);plot(t,y2)
axis([0.5 1 -1 1])


 

  A=magic(5)

imagesc(A)



>>imagesc(A),colorbar,colormap gray;

 

 clf  清屏

close

 

  ========================for if while statements========================
[cpp]  view plain copy
  1. > v=zeros(10,1)  
  2. for i=1:10  
  3. v(i)=2^i;  
  4. end  
  5. v  
v =


     0
     0
     0
     0
     0
     0
     0
     0
     0
     0




v =


           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

[cpp]  view plain copy
  1. indices=1:10;  
  2. indices  
  3. for i=indices, disp(i);  
  4. end  
  5. i=1;  
  6. while i<=5,  
  7. v(i)=100;  
  8. i=i+1;  
  9. end;  
  10. v  
  11. i=1;  
  12. while true,  
  13. v(i)=999;  
  14. i=i+1;  
  15. if i==6,  
  16. break;  
  17. end  
  18. end  
  19. v  
  20. v(1)=2;  
  21. if v(1)==1,  
  22. disp('The value is one');  
  23. else if v(1)==2,  
  24. disp('The value is two');  
  25. else  
  26. disp('The value is neither one or two');  
  27. end  
  28. end
  
addpath('     ')


                                                  vectorization



                                                                                                normal-equation-noninvertibility

pinv()可以计算不可逆的,inv()不可以


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值