>> 1 == 2 % false
ans = 0
>> 1~=2
ans = 1
>> 1&&0
ans = 0
>> 1||0
ans = 1
>> xor(1,0)
ans = 1
>> a =pi
a = 3.1416
>> disp(sprintf('6 decimals: %0.6f',a))
6 decimals: 3.141593
>> format long
>> a
a = 3.141592653589793
>> format short
>> a
a = 3.1416
>> A = [1,2;3,4;5,6]
A =
1 2
3 4
5 6
>> v=[1 ;2 ;3]
v =
1
2
3
>> v= 1:0.1:2
v =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000
>> ones(2,3)
ans =
1 1 1
1 1 1
>> 2*ones(2,3)
ans =
2 2 2
2 2 2
>> zeros(1,3)
ans =
0 0 0
0到1数值随机
>> rand(3,3)
ans =
0.913102 0.320718 0.995509
0.038083 0.707257 0.169677
0.444543 0.864916 0.689890
符合高斯分布的随机数值(平均值为0,方差和标准差为1)
>> randn (1,3)
ans =
-0.71084 0.65244 -0.30712
首先计算大量随机数值 w = -6 +sqrt(10)*(randn(1,10000))
>> hist(w) 画出随机值对应的数量直方图
>> eye(4)
ans =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> A =[1 2;3 4;5 6]
A =
1 2
3 4
5 6
返回的A的大小 为1行2列的矩阵
>> size(A)
ans =
3 2
返回第一个行数
>> size(A,1)
ans = 3
返回第二个列数
>> size(A,2)
ans = 2
>> v=[1 2 3 4]
v =
1 2 3 4
>> length(v)
ans = 4
>> cd 'E:\Coursera_ml_exr\machine-learning-ex1\ex1'
>> dir
. ex1.m featureNormalize.m normalEqn.m
.. ex1_multi.m gradientDescent.m plotData.m
computeCost.m ex1data1.txt gradientDescentMulti.m submit.m
computeCostMulti.m ex1data2.txt lib warmUpExercise.m
load q1y.dat % alternatively, load('q1y.dat')
who % list variables in workspace
whos % list variables in workspace (detailed view)
clear q1y % clear command without any args clears all vars
v = q1x(1:10); % first 10 elements of q1x (counts down the columns)
save hello.mat v; % save variable v into file hello.mat
save hello.txt v -ascii; % save as ascii
A(3,2) % indexing is (row,col)
A(2,:) % get the 2nd row.
% ":" means every element along that dimension
A(:,2) % get the 2nd col
A([1 3],:) % print all the elements of rows 1 and 3
A(:,2) = [10; 11; 12] % change second column
A = [A, [100; 101; 102]]; % append column vec
A(:) % Select all elements as a column vector.
C = [A B] % concatenating A and B matrices side by side
C = [A, B] % concatenating A and B matrices side by side
C = [A; B] % Concatenating A and B top and bottom
A * C % matrix multiplication
A .* B % element-wise multiplication
% A .* C or A * B gives error - wrong dimensions(需要相乘的两个矩阵 维度相同)
A .^ 2 % element-wise square of each element in A
1./v % element-wise reciprocal
log(v) % functions like this operate element-wise on vecs or matrices
exp(v)
abs(v)
-v % -1*v
v + ones(length(v), 1)
% v + 1 % same
A’ % matrix transpose
% max (or min)
val = max(a)
[val,ind] = max(a) % val - maximum element of the vector a and index - index value where maximum occur
val = max(A) % if A is matrix, returns max from each column
% compare values in a matrix & find
a < 3 % checks which values in a are less than 3
find(a < 3) % gives location of elements less than 3
A = magic(3) % generates a magic matrix - not much used in ML algorithms
[r,c] = find(A>=7) % row, column indices for values matching comparison
% sum, prod
sum(a)
prod(a)
floor(a) % or ceil(a)
max(rand(3),rand(3))
max(A,[],1) - maximum along columns(defaults to columns - max(A,[]))
max(A,[],2) - maximum along rows
max(max(A))
or
max(A(:))
将A变成向量求解
按行加和
A = magic(9)
sum(A,1)
sum(A,2)
sum(sum( A .* eye(9) ))
sum(sum( A .* flipud(eye(9)) ))
% Matrix inverse (pseudo-inverse)
pinv(A) % inv(A’*A)*A’
横轴为t向量 纵轴为y
%% plotting
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t,y1);
y2 = cos(2*pi*4*t);
hold on; % "hold off" to turn off
plot(t,y2,'r');
横轴标注
xlabel('time');
纵轴标注
ylabel('value');
图例
legend('sin','cos');
标题
title('my plot');
输出为png文件
print -dpng 'myPlot.png'
关闭图片窗口
close; % or, "close all" to close all figs
打开一号窗口figure(1) 二号窗口figure(2)
figure(1); plot(t, y1);
figure(2); plot(t, y2);
将图片变为1行2列的网格,第一个放一张图
>> subplot(1,2,1); % Divide plot into 1x2 grid, access 1st element
>> plot(t,y1);
>> subplot(1,2,2); % Divide plot into 1x2 grid, access 2nd element
>> plot(t,y2);
改变横轴 纵轴范围
axis([0.5 1 -1 1]); % change axis scale
清空画布
clf;
%% display a matrix (or image)
>> A
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(magic(15)),
imagesc(A),colorbar, colormap gray;
% comma-chaining function calls.
a=1,b=2,c=3
a=1;b=2;c=3;
>> 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 =
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
>> 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 not one or two!
function y = squareThisNumber(x)
y = x^2;
% Navigate to directory:
cd /path/to/function
% Call the function:
functionName(args)
% To add the path for the current session of Octave:
addpath('/path/to/function/')
% To remember the path for future sessions of Octave, after executing addpath above, also do:
savepath
function [y1, y2] = squareandCubeThisNo(x)
y1 = x^2
y2 = x^3
[a,b] = squareandCubeThisNo(x)
>> X = [1 1;1 2;1 3]
X =
1 1
1 2
1 3
>> y = [1;2;3]
y =
1
2
3
>> theta = [0;0]
theta =
0
0
>> j = computeCost(X, y, theta)
j = 2.3333
A .^ 2 % element-wise square of each element in A