機(jī)器學(xué)習(xí)- Matlab 編程常用命令速覽
--總結(jié)自Ng-ML-class Octave/Matlab Tutorial Coursera
from http://blog.csdn.net/yangliuy/article/details/8979793
A、Basic operations and Moving data around
1 在命令行模式用shift + 回車即可附加下一行輸出
2 length命令apply到矩陣時(shí)返回較高的一維的dimension
3 help + 命令是顯示命令的簡(jiǎn)要幫助信息
doc + 命令 是顯示命令的詳細(xì)幫助文檔
4 who 命令 顯示 當(dāng)前所有創(chuàng)建的變量
whos 命令 顯示當(dāng)前所有創(chuàng)建變量的詳細(xì)信息
5 保存變量到.mat 文件save hello.mat b 以二進(jìn)制壓縮保存數(shù)據(jù)
save hello.txt v -ascii 以可讀形式文件保存 即文本格式
6 :means every elements in this col
7 A([1 3], :) 獲取第 1、3兩行所有列的數(shù)據(jù)
8 A = [A, [100; 101; 102]] 在A矩陣后面加一列col vector [100,101,102]
9 size(A) 返回一個(gè)1行2列矩陣 表明第1和第2個(gè)dimensional 的大小
10 C = [A B]等價(jià)于C = [A, B] []為向后面的列添加,連接兩個(gè)矩陣 [] 為concat 連接矩陣或者字符串
11 C= [A; B] ;號(hào)表示向下面行添加,因此會(huì)增加相應(yīng)行數(shù),列數(shù)不變
B、Computing on data
12 A.*B是矩陣/向量點(diǎn)乘 A*B是矩陣相乘
13 log(v) 和exp(v)求以e為底的對(duì)數(shù)和指數(shù)
14 abs()求絕對(duì)值
15 A‘ 求A的轉(zhuǎn)置矩陣
16 max函數(shù)返回矩陣中最大元素的值和索引 [val, ind] = max(a)
17 A < 3 會(huì)判斷A當(dāng)中的每一個(gè)是否小于3,若小于3,對(duì)應(yīng)位置返回true,否則對(duì)于位置返回false
18 find(A<3) 返回矩陣中所有值小于3的索引
19 magic Magic square.
magic(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for all N > 0 except N = 2.
20 [r, c] = find(A >= 7) 返回值大于等于7的element的row及col的索引
21 prod(a) 求矩陣a里面所有元素的乘積
floor(a) 對(duì)矩陣a中元素向下取整
ceil(a)對(duì)矩陣a中元素向上取整
rand(3) 生成3X3的隨機(jī)方陣
max(A,[],1) 求矩陣A的每一列的最大值(最后一維是1表明為dimension 1)
max(A,[],2) 求矩陣A的每一行的最大值
sum(A, 1) 對(duì)矩陣A第一維度(即每列)求和(注意matlab中第一維默認(rèn)是列,然后是行,再然后依次類推。。。)
sum(A, 2) 對(duì)矩陣A第二維度(即每行)求和
sum(sum(A.*eye(9))) 求矩陣A的對(duì)角線元素之和
22 矩陣翻轉(zhuǎn)操作
flipud Flip matrix in up/down direction. 將矩陣上下翻轉(zhuǎn),
類似還有左右翻轉(zhuǎn) fliplr, rot90, flipdim.
flipud(X) returns X with columns preserved and rows flipped
in the up/down direction. For example,
X = 1 4 becomes 3 6
2 5 2 5
3 6 1 4
23 pinv(A) 及inv(A) 求矩陣A 的逆矩陣
C、Plotting data
24 t = [0.1 : 0.01 : 0.98]
y = sin(t)
plot(t, y) 畫正弦曲線
25 hold on; 保留當(dāng)前曲線,畫下一條曲線
26 xlabel 標(biāo)定x軸說明
27 legend('sin','cos') 添加圖例
28 title('my plot') 添加圖片標(biāo)題
29 print -dpng 'myPlot.png' 保存圖片
30 線條顏色標(biāo)注控制
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
31 subplot 畫子圖,即將多個(gè)圖合成一個(gè)圖
>> subplot(1,2,1) % Divides plot a 1X2 grid, access the 1st element
>> plot(t,y1)
>> subplot(1,2,2)
>> plot(t,y2)
32 grid 加上網(wǎng)格
33 figure(1)
>> plot(t, y1)
>> figure(2)
>> plot(t,y2)
將y1與y2的曲線畫到文件名為figure1和figure2的兩個(gè)文件中
34 axis([0.5 1 -1 1]) 設(shè)定x軸范圍為0.5~1,y軸范圍為-1~1
35 clf Clear current figure.
36 imagesc(A)
imagesc Scale data and display as image.
把矩陣A畫成彩色小方格
37 imagesc(A), colorbar, colormap gray;
colorbar 顯示顏色漸變條,表明顏色含義
colormap 設(shè)置colormap性質(zhì) 即RGB三色
A color map matrix may have any number of rows, but it must have
exactly 3 columns. Each row is interpreted as a color, with the
first element specifying the intensity of red light, the second
green, and the third blue. Color intensity can be specified on the
interval 0.0 to 1.0.
38 a = 1; b = 2; c=3; 不會(huì)打出a b c的值
a = 1, b = 2, c=3 會(huì)打出a b c 的值
D、Control statements: for, while, if statements
39 for 循環(huán)
for i = 1:10,
v(i) = 2 ^i;
end;
>> v
v =
2
4
8
16
32
64
128
256
512
1024
>> indices = 1:10
for i = indices,
disp(i)
end;
40 while 循環(huán)
>> while i <= 5,
v(i) = 100;
i = i+1;
end;
>> v
v =
100
100
100
100
100
64
128
256
512
1024
>> i = 1;
>> while true,
v(i) = 999;
i = i + 1;
if i == 6,
break;
end;
end;
>> exit
v =
999
999
999
999
999
64
128
256
512
1024
41 if elseif 判斷分支語句
v(1)=2
v =
2
999
999
999
999
64
128
256
512
1024
>> if v(1) == 1,
disp('The value is one');
elseif v(1) == 2,
disp('The value is two');
else
disp('The value is not one or two.');
end;
The value is two
42 函數(shù)的定義和使用
定義函數(shù) squareThisNumber.m文件
function y = squareThisNumber(x)
y = x^2;
調(diào)用
squareThisNumber(5)
ans =
25
43 addpath 增加matlab搜索函數(shù)的路徑
44 matlab里面定義的函數(shù)可以返回多于一個(gè)值, 這是其與C C++等編程語言
的不同之處,C\C++里面的函數(shù)有唯一返回值
可以允許多個(gè)返回值可以帶來編程上的方便
function [a,b] = squareAndCubeThisNumber(x)
a = x^2;
b = x^3;
調(diào)用
>> [x1,x2] = squareAndCubeThisNumber(5)
x1 =
25
x2 =
125
45 cost function J 函數(shù)示例
function J = costFunctionJ(X, y, theta)
% X is the "design matrix" containing our training examples.
% Y is the class labels
m = size(X,1); % number of training examples
predictions = X*theta; % predictions of hypothesis on all m examples
sqrErrors = (predictions - y).^2; % squared errors
J = 1/(2*m) * sum(sqrErrors);
調(diào)用
X = [1 1; 1 2; 1 3];
y = [1; 2; 3];
theta = [0;1];
>> j = costFunctionJ(X, y, theta)
j =
0
% squared errors is 0 in this example
>> theta = [0;0]
theta =
0
0
>> j = costFunctionJ(X, y, theta)
j =
2.3333
% squared errors is 2.3333 in this example
% which is (1^2 + 2^2 + 3^2) / (2 * 3) = 2.3333
E、Vectorization
Matlab中“向量化”的實(shí)現(xiàn)算法,可以減少不必要的循環(huán),對(duì)向量所有值的計(jì)算
操作一致,這是更快速更高效的算法實(shí)現(xiàn)思路,要注意體會(huì)matlab中“向量化”
“矩陣化”操作變量實(shí)現(xiàn)算法與其他編程語言Java , C, C++的不同