1. 程式人生 > >MATLAB資料處理快速學習教程

MATLAB資料處理快速學習教程

               

本篇內容集合了MATLAB中的基本操作、資料儲存與計算、資料的直線與曲線擬合與畫圖、for-if-while語句的使用方法對一般matlab的操作進行了基本和詳細的應用舉例,非常適合初學者進行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

 figuresubplot(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;

 

 ========================for if while statements========================
> v=zeros(10,1)for i=1:10v(i)=2^i;endv
v =     0     0     0     0     0     0     0     0     0     0v =           2           4           8          16          32          64         128         256         512        1024
indices=1:10;indicesfor i=indices, disp(i);endi=1;while i<=5,v(i)=100;i=i+1;end;vi=1;while true,v(i)=999;i=i+1;if i==6,break;endendvv(1)=2;if v(1)==1,disp('The value is one');else if v(1)==2,disp('The value is two');elsedisp('The value is neither one or two');endend

另外還有一些matlab高階應用: