吴恩达机器学习(第2周--Octave/Matlab Tutorial)【下】

本文介绍使用Matlab进行数据绘制的基本方法,包括简单的正弦波和余弦波绘制、图像显示及颜色调整等,并展示了如何利用for循环、while循环和if语句进行流程控制。此外还涉及了简单的函数编写和调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第2周--Plotting Data

>> t = [0:0.01:0.98];

>> y1 = sin(2*pi*4*t);

>> plot(t,y1)
>> y1 = cos(2*pi*4*t);
>> plot(t,y2)
error: 'y2' undefined near line 1 column 8
>> y1 = sin(2*pi*4*t);
>> y2 = cos(2*pi*4*t);
>> plot(t,y2)
>>
>> plot(t,y1)
>> hold on
>> plot(t,y2,'r')
>> xla
error: 'xla' undefined near line 1 column 1
>> xlabel('time')
>> ylabel('value')
>> legend('sin','cos')
>> title('my plot')
>> print -dpng 'myPlot.png'
>> close
>> figure(1);plot(t,y1);
>> figure(2);plot(t,y2);
>> subplot(1,2,1); % Divides the plot a 1x2 grid, access first element
>> plot(t,y1);
>> subplot(1,2,2);
>> plot(t,y2);
>>
>> axis([0.5 1 -1 1])
>> clf;
>> 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,colormap gray;
>> imagesc(magic(15)), colorbar,colormap gray;
>>
>> a = 1, b = 2, c = 3
a =  1
b =  2
c =  3
>> a = 1; b = 2; c = 3;
>>

第2周--Control Statements: for, while, if statement

>> 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
    6
    8
   10
   12
   14
   16
   18
   20

>> 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 = 1:indices
disp(i);
end;
 1
>> 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
>>
>>
>> edit squareThisNumber(x).m

>> squareThisNumber(5)
ans =  25
>> edit squareAndCubeThisNumber.m

>> [a,b] = squareAndsquareAndCubeThisNumber(5);
error: 'squareAndsquareAndCubeThisNumber' undefined near line 1 column 8
>> [a,b] = squareAndCubeThisNumber(5);
>> a
a =  25
>> b
b =  125
>> x = [1 1;1 2; 1 3]
x =

   1   1
   1   2
   1   3

>> y = [1;2;3]
y =

   1
   2
   3

>> theta = [0;1];
## Copyright (C) 2018 Administrator
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.


## -*- texinfo -*- 
## @deftypefn {} {@var{retval} =} costFunctionJ (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn


## Author: Administrator <Administrator@F4HFJA67XY3IIM1>
## Created: 2018-07-02


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
  predications = X*theta; %predications of hypothesis on all m
  % examples
  sqrErrors = (predications - y) .^2; % squared errors
  
  J = 1/(2*m) * sum(sqrErrors);
  
endfunction
>> edit costFunctionJ.m

>> X = [1 1;1 2; 1 3]
X =

   1   1
   1   2
   1   3

>> y = [1;2;3]
y =

   1
   2
   3

>> theta = [0;1];
>> j = costFunctionJ(X,y,theta)
j = 0
>> theta = [0;0];
>> j = costFunctionJ(X,y,theta)
j =  2.3333
>> (1^2 + 2^2 +3^2)/(2*3)
ans =  2.3333
>>

第2周--Vectorization






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值