Basic Operations 基本操作
a =
1.0000 15.0000 2.0000 0.5000
% ================format ================
>> format long
>> a
a =
1.000000000000000 15.000000000000000 2.000000000000000 0.500000000000000
% ================fprintf ================
>> fprintf('%f %f \n',1, 2);
1.000000 2.000000
% ================ ================
>> v=1:0.1:2 % start;步長;end
v =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000
>> v=1:6
v =
1 2 3 4 5 6
% ================help ================
help
A=rand(3,2);
A=randn(3,2);
w = -6 + sqrt(10)*(randn(1,10000));
hist(w); %柱狀圖
eye();
ones();
zeros(1,3)
Moving Data Around 移動數據
% ================A ================
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
% ================ size ================
>>size(A) %返回矩陣維度:行數 列數
ans = 3 2
>>size(A,1) %返回矩陣維度:行數
ans = 3
>>size(A,2) %返回矩陣維度:列數
ans = 2
% ================ length================
>>v = [1 2 3 4] %返回矩陣維度:行數
v =
1 2 3 4
>>length(v) %返回矩陣最大維度大小
ans = 4
>>length(A) %返回矩陣最大維度大小
ans = 3
% ================ ================
pwd %當前路徑
ls %當前路徑下的文件
cd %切換目錄
% ================ load clear save data ================
data = load('ex1data2.txt');
who %當前工作空間的所有變量
whos %當前工作空間的所有變量的詳細信息
clear variablename %刪除變量
clear variable %刪除當前工作空間的所有變量
>> save test.mat A
>> save testtext.txt A –ascii % save as text (ASCII)
% ================ 矩陣 Matrix================
>> 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
>> 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
Computing on Data 計算數據
% matlab默認 * / 是矩陣操作 .* ./是對每個元素操作
>>A
ans =
1 2
3 4
5 6
>>A.*B %對應位置的數據相乘,即element product
ans =
11 24
39 56
75 96
>>A.^2 % 對應位置的數據的平方
ans =
1 4
9 16
25 36
>>1 ./A % 對應位置的數據的除法
ans =
1.00000 0.50000
0.33333 0.25000
0.20000 0.16667
>>-A % -1 * A
ans =
-1 -2
-3 -4
-5 -6
>>abs(A) % 絕對值
>> A + 1 % 或者 A + ones(3,2)
ans =
2 3
4 5
6 7
>>A'%轉置
ans =
1 3 5
2 4 6
>> A<3 %小於3的元素對應位置爲1(真),否則爲0(假)
ans =
1 1
0 0
0 0
>> find(A<3) % A中哪些元素小於3:第一個,第四個(從上到下數,列優先)
ans =
1
4
A =
8 1 6
3 5 7
4 9 2
>> [r,c]=find(A>=6) %r是行 c是列 所以大於等於6的數的位置是 第一行第一列,第3行第2列。。。
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
>> [val,ind] = max(a) % val是a中最大的數的值,ind是這個值的位置
val =
15
ind =
2
>> sum(a)
ans =
18.5000
>> sum(A) % 默認求每列的和,相當於sum(A,1)
% 1 — Default. Returns sum of elements in each column.
% 2 — sum(A,2)Returns sum of elements in each row.每行的和
ans =
9 12
>> prod(a) %a中元素的乘積
ans =
15
>> prod(A) %求每列的乘積,相當於prod(A,1)
% 1 — Default. Returns prod of elements in each column.
% 2 — prod(A,2)Returns prod of elements in each row.每行的乘積
ans =
15 48
>> 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)) %A中最大元素
ans =
9
>> A(:)
ans =
8
3
4
1
5
9
6
7
2
>> max(A(:))
ans =
9
>> A=magic(9) % 行,列,對角線加起來的值相等的9*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)%column sum
ans =
369 369 369 369 369 369 369 369 369
>> sum(A,2)%sum each 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))) %sum(sum(A.*flipud(eye(9))))
ans =
369
>> flipud(A) %returns X with the order of elements flipped upside down along the first dimension. 列數據顛倒,第一行的到最後一行,第二行到倒數第二行,以此類推
ans =
5 6
3 4
1 2
>> 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
Plotting Data 繪圖數據
>> t=[0:0.01:0.98];
>> y1=sin(2*pi*4*t);
>> plot(t,y1)
>> hold on;%plot new figure on the old ones
>> y2=cos(2*pi*4*t);
>> plot(t,y2,'r')
>> xlabel('time') %x座標標籤
>> ylabel('value') %y座標標籤
>> legend('sin','cos') %圖例
>> title('my plot')
>> print -dpng 'myplot.png' %save as a file in default catalog
>> cd 'C:\Users\x\Desktop'; print -dpng 'myplot.png' % 保存到相應的路徑
>> close %關閉圖片
%分別顯示兩幅圖像
>> figure(1);plot(t,y1);
>> figure(2);plot(t,y2)
%一幅圖中顯示兩個subplot figure
subplot(1,2,1); % Divides plot a 1* 2 grid, access fisrt element
plot(t,y1);
subplot(1,2,2);% Divides plot a 1* 2 grid, access second element
plot(t,y2);
axis([0.5 1 -1 1]) %改變正在編輯的圖的x座標範圍爲[0.5 1]y座標範圍爲[-1 1]
clf % clear figure
>> A=magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> imagesc(A) %可視化矩陣
>> imagesc(A), colorbar
>> imagesc(A), colorbar, colormap gray; % 逗號運算符,多個命令運行
>> a=1;b=2;c=3; %多個命令運行,但不會輸出
>> a=1,b=2,c=3 % 逗號運算符,多個命令運行,有輸出
a =
1
b =
2
c =
3
Control Statements: for, while, if statement 控制語句:for,while,if 語句
>> v = zeros(10,1)
v =
0
0
0
0
0
0
0
0
0
0
>> for i = 1:10,
v(i) = 2^i;
end;
>> v
v =
2
4
8
16
32
64
128
256
512
1024
% ================ ================
>> indices = 1:10;
>> indices
indices =
1 2 3 4 5 6 7 8 9 10
>> for i = indices,
disp(i);
end;
1
2
3
4
5
6
7
8
9
10
% ================ ================
>> i = 1;
>> 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;
>> v
v =
999
999
999
999
999
64
128
256
512
1024
% ================ ================
>> v(1)
ans =
999
>> v(1) = 2;
>> 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
% ================ function 函數 ================
function [y1, y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;
end
[a, b] = squareAndCubeThisNumber(5);
>>b
b = 125
% ================代價函數方程 ================
function J = computeCostMulti(X, y, theta)
%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables
% J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = sum((X * theta - y).^2)/(2*m);
% =========================================================================
end
Vectorization 向量化
下圖右邊的是用C++的庫:
梯度下降
Normal Equation Noninvertibility 正規方程 不可逆性
不可逆性矩陣:奇異矩陣,退化矩陣
計算逆矩陣有兩個函數:pinv 和inv
區別:
- 僞逆函數pinv可以計算出
不可逆的原因:
- 特徵之間不線性獨立
- 特徵數量大於訓練集的數量