转自:http://blog.csdn.net/abcjennifer/article/details/7706581
本篇内容集合了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*3的random矩阵,每个值在[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)) %在两个random的3*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;
- > v=zeros(10,1)
- for i=1:10
- v(i)=2^i;
- end
- v
v =
0
0
0
0
0
0
0
0
0
0
v =
2
4
8
16
32
64
128
256
512
1024
- indices=1:10;
- indices
- for i=indices, disp(i);
- end
- i=1;
- while i<=5,
- v(i)=100;
- i=i+1;
- end;
- v
- i=1;
- while true,
- v(i)=999;
- i=i+1;
- if i==6,
- break;
- end
- end
- v
- v(1)=2;
- if v(1)==1,
- disp(\'The value is one\');
- else if v(1)==2,
- disp(\'The value is two\');
- else
- disp(\'The value is neither one or two\');
- end
- end
另外还有一些matlab高级应用: