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++的库:
梯度下降
theta=theta?alpha/m?X′?(X?theta?y);
Normal Equation Noninvertibility 正规方程 不可逆性
不可逆性矩阵:奇异矩阵,退化矩阵
计算逆矩阵有两个函数:pinv 和inv
区别:
- 伪逆函数pinv可以计算出θ,即使 X’ * X 不可逆
不可逆的原因:
- 特征之间不线性独立
- 特征数量大于训练集的数量
时间: 2024-11-05 18:33:54