Matlab基本操作、矩阵输入、代码基础、变量、文件读写

1. 基本操作

  1. 基本运算符
+ - * / ^
  1. 运算顺序

​ 括号、乘方、乘除、加减,从左到右

  1. 变量类型

    类型
    logical
    char
    numericint/uint 8,16,32,64; single; double
    cell
    struct
    function handle
  2. 查看变量

​ who, whos

  1. 特殊变量和常数

    ans
    i/j
    Inf
    eps
    NaN
    pi
  2. 查看关键字

    iskeyword

  3. 呼叫优先级

    变量>内置函数>子函数>私有函数(Mex-file, P-file, M-file)

  4. format指令

    指定数据输出(显示的,存储方式不变)类型:format short/long/shortE/longE/bank/hex/rat,rat转为有理数

2. 矩阵输入

  1. 数组(向量、矩阵)

    a=[1 2 3 4] %行向量
    b=[1;2;3;4] %列向量
    a*b %矩阵乘法
    
  2. 数组索引

    A = [1 2 3; 4 5 6; 7 8 9]
    %% 方法1,单索引
    A(2)% 按列索引,按列第2个,显示4
    A([1 3 5])% 第1、3、5个,1 7 5
    A([1 3;1 3])%两行都为A的第1、3个,[1 7;1 7] 
    
    %%方法2,行列索引
    A(2,1)%2行1列,显示4
    A([1 3],[1 3])%1、3行&1、3列的交集
    
  3. 切片操作

    B=[[1:1:10];[11:1:20]] % 等差为1,从1到10的2行行向量(左右闭合)
    B(2,:)=[] % 把第二行删掉
    
  4. 拼接矩阵(增广矩阵)

    A=[0 1 2; 3 2 1]
    B=[[1:3];[4:6]]
    F = [A,B] %行数不变,添加列
    X = [A;B] %列数不变,添加行
    
  5. 矩阵的基本运算

    + - * / ^ . '
    

    * 矩阵乘法

    .* 点乘:同索引相乘

    / 矩阵除法: A / B ≈ A ∗ i n v ( B ) A/B\approx A*inv(B) A/BAinv(B),其中inv为求逆矩阵

    ./ 点除(左除右):同索引相除

    .\ 点除(右除左)

    ^ 次方:A*A*A*…

    .^ 点次方:每个索引的次方

  6. 一些特殊矩阵

    linspace(start,end,num)%从start到end分成num个
    eye(n)
    zeros(n1,n2)
    ones(n1,n2)
    diag([x1 x2 x3])%对角线矩阵
    rand()
    
  7. 一些矩阵相关函数

    %% 按列做运算
    max(A) % 每一列中最大的数(按列做运算)
    max(max(A)) % 得到矩阵的最大数
    min(A)
    sum(A) %每一列所得的和
    mean(A) %每一列所得的平均值
    sort(A) %每一列从小到大往下排序(每一行不绑定)
    sortrows(A) % 按第一列的数值,把行做排序(每一行是绑定的)
    size(A) % 对矩阵rows columns
    length(A) % 对向量
    find(A==5) % 找出5的索引 
    

3. Matlab在工程中的应用

  1. Scrip writing脚本编程

    不用compile就可以执行,是解释性语言(一行行读Scrip去执行)

  2. 结构化程序

Flow ControlInterpreter
if, elseif, else
for
switch, case, otherwise
try, catch
while
break
continue
end
pause
return
相关逻辑运算含义
<
<=
>
>=
==
~=
&&
||

预分配空间给变量,节省计算时间

A = zeros(2000,2000)
  1. Scrips vs. Functions

    1. 写函数时候运算注意用点乘

    2. 函数的默认变量

    变量名含义
    inputname
    mfilename
    nargin函数输入参数个数
    nargout函数输出参数个数
    varargin
    varargout
    1. Function Handles

      f=@(x) exp(-2*x) % 
      x = 0:0.1:2
      y = f(x)
      plot(x,y)
      

    4. 变量

    ​ logical、numeric、string、structure、cell、function handle

    1. String

      默认类型为double

      类型转换:int8()…

      str = 'aardads'
      % 比较个别的character
      'a' == str % str中'a'的位置为1
      str('a' == str) = 'Z' % 把'Z'赋值给 str索引为true(1)的位置
      
      % 比较整个string
      strcmp()
      
    2. Structure

      student.name = 'adsad'
      student.id='213123'
      student.number=3123532
      student.grade=[1,2,3;...
      4,5,6;...
      3,5,7]
      
      %添加新同学
      student(2).name='sfdcx'
      student(2)/id='236431'
      ......
      

      结构体相关函数

      名称含义
      cell2struct
      fieldnames
      getfield
      isfield
      isstruct
      orderfields
      rmfields
      rmfield清除struct中的field
      struct
      struct2cell
      structfun
    3. Cell Array(元胞数组)

      作为指针,指到不同数据类型

      和矩阵类似,但每个单元数据类型不同

      % 创建方式1
      A(1,1) = {[1,2;3 4; 5 6]}
      A(1,2) = {'ANNE SIMITH'}
      A(2,1) = {3+7i}
      A(2,2) = {-pi:pi:pi}
      
      % 创建方式2
      A{1,1} = [1,2;3 4; 5,6]
      A{1,2} = 'ANNE SMITH'
      A{2,1} = 3+7i
      A{2,2} = -pi:pi:pi
      
      % 想要取到2
      A{1,1}(1,2)
      
      NAME含义
      cell
      cell2mat
      cell2struct
      celldisp
      cellfun
      cellplot
      cellstr
      iscell
      mat2cell矩阵转cell
      num2cellP4, 55:00
      struct2cell

​ concatenation

​ cat()

​ reshape()

  1. 文件读写

    把WorkSpace存贮到file,把file读到WorkSpace

    Mat格式,txt:load,save

    clear;
    a = magic(4); %变量放到workspace
    save mydata1.mat % 把workspace数据存储到mat
    save mydata2.mat -ascii % -ascii确保存储的是数值,可阅读的文字
    
    load('mydata1.mat')
    load('mydata2.mat','-ascii')
    

    xls/xlsx格式:xlsread,xlswrite

    % 存储特定的变量
    score = xlsread('xxx.xlsx')
    score = xlsread('xxx.xlsx','B2:D4')
    
    M = mean(score);
    xlswrite('xxx.xlsx',M,1,'E2:E4')
    xlswrite('xxx.xlsx',{'Mean'},1 ,'E1')
    
    [score header] = xlsread('xxx.xlsx')
    
    %低阶的IO文件函数
    fid = fopen('','')
    fclose(fid)
    
    fscanf()
    fprintf()
    feof()
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值