[MITOPENCOURSEWARE] 6.094 Introduction to Programming in MATLAB

Introduction to MATLAB

Lecture 1 Variables, Scripts, and Operations

  • Matlab Basic

    • MATLAB can be thought of as a super-powerful graphing calculator
    • In addition it is a programming language
  • 一些命令

    • 帮助命令
      • help [ ]
      • doc [ ] 更详细的帮助
    • 注释 %

      第一行注释为脚本的帮助文档

    • 新建脚本 edit

      edit [ ].m

    • 输出字符串disp

      注意字符串使用单引号

    • 保存变量到文件中save

      save myFile a b

      • saves variables a and b to the file myfile.mat
      • myfile.mat file is saved in the current directory
    • 移除当前环境中变量 clear
    • 加载文件中变量load
    • 清理命令窗口clc
  • 变量

    • 创建和修改的变量会一直保存在工作区
    • Matlab is a weekly typed language. 不需要初始化变量
    • 变量命名:首位必须是字母;以字母、数字、下划线_ 组成
    • 大小写敏感
    • Built-in variables. Don’t use these names!
      • i and j can be used to indicate complex numbers
      • pi has the value 3.1415926…
      • ans stores the last unassigned value (like on a calculator)
      • Inf and -Inf are positive and negative infinity
      • NaN represents ‘Not a Number’
  • 若不想显示输出,则在句末加上;

  • 数组[ ]

    Two types of arrays

    1. matrix of numbers (either double or complex)
    2. cell array of objects (more advanced data structure)
    • 行向量:在[ ] 中使用逗号或空格分隔值
    • 列向量:在[ ]中使用;分隔
    • size( )显示矩阵大小
    • length( )得到矩阵长度
    • Initialize a vector of oneszeros, or random numbers
      » o=ones(1,10)
      row vector with 10 elements, all 1
      » z=zeros(23,1)
      column vector with 23 elements, all 0
      » r=rand(1,45)
      row vector with 45 elements (uniform [0,1])
      » n=nan(1,69)
      row vector of NaNs (useful for representing uninitialized variables)
    • To initialize a linear vector of values use linspace
      » a=linspace(0,10,5)
      starts at 0, ends at 10 (inclusive), 5 values
      Can also use colon operator (:)
      » b=0:2:10
      starts at 0, increments by 2, and ends at or before 10
      increment can be decimal or negative
      » c=1:5
      if increment isn’t specified, default is 1
    • To initialize logarithmically spaced values use logspace
      similar to linspace, but generate logarithmically spaced vectors
    • Vector Indexing
      • Matlab 索引从0开始
      • The index argument can be a vector
      • 使用maxmin得到最大/小值和索引号
      • 使用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值