5.1 Basic operations
5.2 Moving data around
5.3 Computing on data
5.4 Plotting data
5.5 For while if statement and functions
5.6 Vectorization
看过前两节课后,觉得matlab语言貌似和octave没差, 因此第五节学习打算利用matlab进行学习
基本操作
???怎么代码块都是一个颜色??```matlab 识别不出来??丑…
>> 5 + 6ans = % matlab命令行中 ans 就是这个样子,占地方就占地方吧,不愿修改格式qaq11 >> 3 - 2ans =1>> 5 * 8ans =40>> 2^6ans =64>> 1 == 2 %falseans =0>> 1 ~= 2 %true [注] 这里不等于写作 ~= 而不是 != (区别于其他编程语言) ans =1>> 1 && 0 %ANDans =0>> 1 || 0 %ORans =1>> xor(1,0)ans =1
变量
>> a = 3a =3>> a = 3; % semicolon ";" supressing output 不输出a
>> b = 'hi';
>> c = (3 >= 1);
>> cc =1>> bb =hi>> a = pi;
>> aa =3.1416>> disp(a) %disp3.1416>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14>> disp(sprintf('6 decimals: %0.6f', a))
6 decimals: 3.141593>> format long %format
>> aa =3.141592653589793>> format short
>> aa =3.1416
向量和矩阵
>> A = [1 2; 3 4; 5 6]A =1 23 45 6>> A = [1 2; %另一种输入矩阵A的方式
3 4;
5 6]A =1 23 45 6>> v = [ 1 2 3] %行向量v =1 2 3>> v = [1; 2; 3] %列向量v =123>> v = 1: 0.1: 2 %从 1 到 2 步长为 0.1v =1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000>> v = 1 : 6v =1 2 3 4 5 6>> ones (2, 3) %ones 全 1 矩阵ans =1 1 11 1 1>> C = 2*ones(2, 3)C =2 2 22 2 2>> w = zeros(1, 3) %zeros 全 0 矩阵w =0 0 0>> w = rand(1, 3) %rand 0~1 随机数w =0.8147 0.9058 0.1270>> w = rand(3, 3)w =0.9134 0.2785 0.96490.6324 0.5469 0.15760.0975 0.9575 0.9706>> w = randn(1, 3) %randn 高斯随机数w =0.7254 -0.0631 0.7147>> w = -6 + sqrt(10)*(randn(1, 10));
>> ww =-5.6077 -1.4568 -12.2009 -6.6252 -9.8195 3.1959 -3.3904 -1.6393 -9.3463 -7.4819>> hist(w) %hist 绘w的直方图 (图在代码后给出)
>> w = -6 + sqrt(10)*(randn(1, 100));
>> hist(w)
>> w = -6 + sqrt(10)*(randn(1, 10000));
>> hist(w)
>> hist(w,50) %规定列数
>> eye(4) %eye 单位矩阵ans =1 0 0 00 1 0 00 0 1 00 0 0 1>> help eye %help文档eye Identity matrix.eye(N) is the N-by-N identity matrix.eye(M,N) or eye([M,N]) is an M-by-N matrix with 1's onthe diagonal and zeros elsewhere.eye(SIZE(A)) is the same size as A.eye with no arguments is the scalar 1.eye(..., CLASSNAME) is a matrix with ones of class specified byCLASSNAME on the diagonal and zeros elsewhere.eye(..., 'like', Y) is an identity matrix with the same data type, sparsity,and complexity (real or complex) as the numeric variable Y.Note: The size inputs M and N should be nonnegative integers. Negative integers are treated as 0.Example:x = eye(2,3,'int8');See also speye, ones, zeros, rand, randn.eye 的参考页名为 eye 的其他函数>>
w = -6 + sqrt(10)*(randn(1, 10));
w = -6 + sqrt(10)*(randn(1, 100));
w = -6 + sqrt(10)*(randn(1, 10000));
>> clear %清除工作区中所有变量
>> A = [1 2;3 4; 5 6]A =1 23 45 6>> size(A) %size 矩阵 A 大小ans =3 2>> sz = size(A)sz =3 2>> size(sz)ans =1 2>> size(A, 1) %A矩阵的第一维度的大小 即矩阵行数ans =3>> size(A, 2) %A矩阵的第二维度的大小 即矩阵列数ans =2>> v = [1 2 3 4]v =1 2 3 4>> length(v) %最大维度的大小ans =4>> length(A) %但一般 length 多用于求向量长度ans =3>> length([1; 2; 3; 4; 5])ans =5>> pwd %pwd 当前路径ans =C:\Users\asus\Documents\MATLAB>> cd 'C:\Users\asus\Desktop' %cd 修改路径(到桌面Desktop)
>> ls %ls 当前路径下文档2020winter Sublime Text 3.lnk lab01.zip
Adobe Dreamweaver CC 2018.lnk Typora.lnk >> load featureX.dat
>> load priceY.dat
>> load('featureX.dat')
>> who %列出所有变量您的变量为: %汉化了的matlabA ans featureX priceY sz v >> whos %列出所有变量详细信息Name Size Bytes Class AttributesA 3x2 48 double ans 1x30 60 char featureX 19x2 304 double priceY 19x1 152 double sz 1x2 16 double v 1x4 32 double >> featureXfeatureX =2104 31400 32400 31416 23000 41985 41534 31427 31330 31494 31940 42000 31890 34478 51238 32300 41320 21236 32609 4>> size(featureX)ans =19 2>> size(priceY)ans =19 1>> clear featureX %清除某一变量
>> whosName Size Bytes Class AttributesA 3x2 48 double ans 1x2 16 double priceY 19x1 152 double sz 1x2 16 double v 1x4 32 double >> v = priceY(1:10) %取priceY前十列v =3399329936092320539929993149198921202425>> who您的变量为:A ans priceY sz v >> whosName Size Bytes Class AttributesA 3x2 48 double ans 1x2 16 double priceY 19x1 152 double sz 1x2 16 double v 10x1 80 double >> save hello.mat v; %save 存储文件
>> clear
>> load hello.mat
>> whosName Size Bytes Class Attributesv 10x1 80 double >> vv =3399329936092320539929993149198921202425>> save hello.txt v -ascii %save as text(ASCII)
hello.txt
索引
>> A = [1 2; 3 4; 5 6]A =1 23 45 6>> A(3, 2) %indexans =6>> A(2,:) % ":" means every element along that row/columnans =3 4>> A(:,2)ans =246>> A([1 3], :) %第一行和第三行ans =1 25 6>> A(:,2) = [10; 11; 12] %替换列A =1 103 115 12>> A = [A, [100; 101; 102]] %append another column vector to the rightA =1 10 1003 11 1015 12 102>> A(:) %把A的所有元素放入一个列向量ans =135101112100101102>> A = [1 2; 3 4; 5 6]A =1 23 45 6>> B = [11 12; 13 14; 15 16]B =11 1213 1415 16>> C = [A B] %左右重组C =1 2 11 123 4 13 145 6 15 16>> C = [A; B] %上下重组C =1 23 45 611 1213 1415 16>> size(C)ans =6 2
>> A = [1 2; 3 4; 5 6]A =1 23 45 6>> B = [11 12; 13 14; 15 16]B =11 1213 1415 16>> C = [1 1; 2 2]C =1 12 2>> A*Cans =5 511 1117 17>> A .* B %A,B相对应的元素相乘 .一般表示元素之间的运算ans =11 2439 5675 96>> AA =1 23 45 6>> A .^ 2 %这里^ 与 2 之间要加空格!ans =1 49 1625 36>> v = [1; 2; 3]v =123>> 1 ./ vans =1.00000.50000.3333>> 1 ./ Aans =1.0000 0.50000.3333 0.25000.2000 0.1667>> log(v)ans =00.69311.0986>> exp(v)ans =2.71837.389120.0855>> abs(v)ans =123>> abs([-1; -1; -3])ans =113>> -vans =-1-2-3>> v + ones(length(v),1) %对v中每个元素加一ans =234>> v + 1 %对v中每个元素加一的另一种方法ans =234>> A' %转置ans =1 3 52 4 6>> (A')'ans =1 23 45 6>> a = [1 15 2 0.5]a =1.0000 15.0000 2.0000 0.5000>> val = max(a) %A中元素最大值val =15>> [val, ind] = max(a) %ind=index 索引 最大值元素的位置val =15ind =2>> max(A) %A的每一列的最大值ans =5 6>> a < 3 %a的每一个元素与3进行比较ans =1 0 1 1>> aa =1.0000 15.0000 2.0000 0.5000>> find(a < 3) %返回值为其索引值ans =1 3 4>> A = magic(3) % magic矩阵 每行每列元素的和都是同一个数A =8 1 63 5 74 9 2>> [r, c] = find( A >= 7) %r=row c=columnr =132c =123>> aa =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>> ceiling(a)
未定义函数或变量 'ceiling'。%Andrew Ng老师说ceiling=ceil 但在matlab中没有这个命令 可能仅适用于octave>> rand(3) %3*3随机矩阵ans =0.0581 0.1216 0.30250.3230 0.7500 0.60230.8535 0.4727 0.2124>> max(rand(3),rand(3)) %由两个3*3矩阵中对应较大元素组成的ans =0.6958 0.7344 0.75140.8546 0.9334 0.99370.3971 0.8652 0.4279>> AA =8 1 63 5 74 9 2>> max(A,[],1) %A矩阵每一列最大值ans =8 9 7>> max(A,[],2) %A矩阵每一行最大值 max默认求每一列最大值ans =879>> max(max(A)) %求A中最大元素ans =9>> A(:)ans =834159672>> max(A(:)) %求A中最大元素另一种方法ans =9>> A = magic(9)A =47 58 69 80 1 12 23 34 4557 68 79 9 11 22 33 44 4667 78 8 10 21 32 43 54 5677 7 18 20 31 42 53 55 666 17 19 30 41 52 63 65 7616 27 29 40 51 62 64 75 526 28 39 50 61 72 74 4 1536 38 49 60 71 73 3 14 2537 48 59 70 81 2 13 24 35>> sum(A,1) %取A的每一列相加ans =369 369 369 369 369 369 369 369 369>> sum(A,2) %取A的每一行相加ans =369369369369369369369369369>> eye(9)ans =1 0 0 0 0 0 0 0 00 1 0 0 0 0 0 0 00 0 1 0 0 0 0 0 00 0 0 1 0 0 0 0 00 0 0 0 1 0 0 0 00 0 0 0 0 1 0 0 00 0 0 0 0 0 1 0 00 0 0 0 0 0 0 1 00 0 0 0 0 0 0 0 1>> A .* eye(9)ans =47 0 0 0 0 0 0 0 00 68 0 0 0 0 0 0 00 0 8 0 0 0 0 0 00 0 0 20 0 0 0 0 00 0 0 0 41 0 0 0 00 0 0 0 0 62 0 0 00 0 0 0 0 0 74 0 00 0 0 0 0 0 0 14 00 0 0 0 0 0 0 0 35>> sum(sum(A .* eye(9))) %对角线之和ans =369>> sum(sum(A .*flipud(eye(9)))) %flipud矩阵垂直翻转 对另一条对角线求和ans =369>> A = magic(3)A =8 1 63 5 74 9 2>> pinv(A) %INVERSE 求A的逆ans =0.1472 -0.1444 0.0639-0.0611 0.0222 0.1056-0.0194 0.1889 -0.1028>> temp = pinv(A)temp =0.1472 -0.1444 0.0639-0.0611 0.0222 0.1056-0.0194 0.1889 -0.1028>> temp * Aans =1.0000 0.0000 -0.0000-0.0000 1.0000 0.00000.0000 -0.0000 1.0000
可视化
>> t = [0: 0.01: 0.98];
>> y1 = sin(2 * pi * 4 * t);
>> plot(t, y1);
>> y2 = cos(2 * pi * 4 * t);
>> plot(t, y2);
>> plot(t, y1);
>> hold on; %在同一个图绘制两条曲线
>> plot(t, y2); %plot(t, y2, 'r') 红色线
>> xlabel('time')
>> ylabel('value')
>> legend('sin','cos')
>> title('mt plot')
>> print -dpng 'myPlot.png' %保存到本地
>> close %关闭图像
>> figure(1);plot(t, y1);
>> figure(2);plot(t, y2); %两个图像
>> subplot(1, 2, 1); %Divides plot a 1*2 grid, access first element 将图像分成1*2的网格,现在使用第一个格子
>> plot(t, y1);
>> subplot(1, 2, 2);
>> plot(t, y2);
>> axis([0.5 1 -1 1]) %改变x轴和y轴
>> clf; %清除一个图像
>> A = magic(5)A =17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9>> imagesc(A) %可视化一个矩阵 不同的颜色对应不同的值
>> imagesc(A), colorbar, colormap gray;
>> imagesc(magic(15)), colorbar, colormap gray;
控制语句
>> v = zeros(10, 1)v =0000000000>> for i = 1 : 10, %这里空格没有意义, 只是为了让结构更加明显v(i) = 2^i; % forend;
>> vv =2481632641282565121024>> indices = 1 : 10;
>> indicesindices =1 2 3 4 5 6 7 8 9 10>> for i = indices,disp(i);end;12345678910
>> vv =2481632641282565121024>> i = 1;
>> while i <= 5, %whilev(i) = 100;i = i + 1;end;
>> vv =100100100100100641282565121024>> i = 1;
>> while true,v(i) = 999;i = i + 1;if i == 6, break; %breakend;end;
>> vv =999999999999999641282565121024
函数
>> pwdans =C:\Users\asus\Documents\MATLAB>> cd 'C:\Users\asus\Desktop'
%在桌面上事先建立一个suqareThisNumber.m文档, 用写字板打开后是一个函数%function y = suqareThisNumber(x)%y = x^2;%这里推荐用写字板而不是记事本打开文件>> squareThisNumber(5)ans =25>> addpath('C:\Users\asus\Desktop') %不必修改路径, 而是将路径添加到搜索路径中
>> cd 'C:\'
>> squareThisNumber(5)ans =25>> pwdans =C:\
>> %返回多个值
%建立一个文件在桌面 squareAndCubeThisNumber.m 内容如下
%function [y1, y2] = squareAndCubeThisNumber(x)%y1 = x^2;
%y2 = x^3;>> [a, b] = squareAndCubeThisNumber(5);
>> aa =25>> bb =125
Cost function
%costFunction.m
function J = costFunctionJ(X, y, theta)%X is the "design matrix" containing our training examples.%y is the class labelsm = size(X, 1); %Number of training examples.
predictions = X*theta; %Predictions of hypothesis on all m examples.
sqrErrors = (predictions - y) .^ 2; %square errors.J = 1/(2*m) * sum(sqrErrors);
>> X = [1 1; 1 2; 1 3]X =1 11 21 3>> y = [1; 2; 3]y =123>> theta = [0; 1];
>> j = costFunction(X, y, theta)j =0>> theta = [0; 0];
>> j = costFunction(X, y, theta)j =2.3333
向量化