• MATLAB——元胞数组


    一、

    1、元胞数组的创建

    >>  a={'matlab',20;ones(2,3),1:10}
    
    a = 
    
        'matlab'        [         20]
        [2x3 double]    [1x10 double]>>  a={'matlab',20;ones(2,3),1:10}
    >>  b=[{'matlab'},{20};{ones(2,3)},{1:10}]
    
    b = 
    
        'matlab'        [         20]
        [2x3 double]    [1x10 double]

    a和b是等价的

    >>  c={10}
    c = 
        [10]
    

    >> c(2,2)={5}

    c =

    [10] []
    [] [5]

    >> c(1,2)={2}

    c =

    [10] [2]
    [] [5]

    clear all
    
    a={'matlab',20;ones(2,3),1:10}
    b=[{'matlab'},{20};{ones(2,3)},{1:10}]
    c={10}
    c(1,2)={2}
    c(2,2)={5}
    c(2,1)=3

    赋值需用花括号,下标用小括号。

    >> isequal(a,b)
    
    ans =
    
         1
    >> whos
      Name      Size            Bytes  Class      Attributes
    
      a         2x2               596  cell                 
      ans       1x1                 1  logical              
      b         2x2               596  cell                 
      c         2x2               480  cell        
    >> d=cell(3,3)
    
    d = 
    
        []    []    []
        []    []    []
        []    []    []

    2、元胞数组访问

    >> str=a(1,1)
    
    str = 
    
        'matlab'
    
    >> class(str)
    
    ans =
    
    cell
    >> str=a{1,1}
    
    str =
    
    matlab
    
    
    >> class(str)
    
    ans =
    
    char
    >> a{2,2}
    
    ans =
    
         1     2     3     4     5     6     7     8     9    10
    
    >> a(2,2)
    
    ans = 
    
        [1x10 double]

    ()和{}有着本质的区别,大括号用于表示元胞的内容,小括号表示指定的元胞。

    >> a(2,2)(2)
    错误: ()-索引必须显示在索引表达式的最后。
     
    >> a{2,2}(2)
    
    ans =
    
         2
    >> b=c{2:3,2:3}
    
    b =
    
         5
    
    >> b=c(2:3,2:3)
    
    b = 
    
        [5]    [6]
        [8]    [9]

    b相当于从c中获取了四个元胞,用a的例子更好理解

    >> d=a(2,:)
    
    d = 
    
        [2x3 double]    [1x10 double]

    >> d=a(2,2)
    
    d = 
    
        [1x10 double]
    
    >> d=a{2,2}
    
    d =
    
         1     2     3     4     5     6     7     8     9    10

    3、元胞数组的删除和重塑

         要删除单元数组中的行或列,可以用冒号表示单元数组中的行或列,然后对其赋一个空矩阵即可。

    >> a={20,'matlab';ones(2,3),1:3}
    
    a = 
    
        [        20]    'matlab'    
        [2x3 double]    [1x3 double]
    

    >> a{1,2}=[]

    a =

    [ 20] []
    [2x3 double] [1x3 double]

    >> a{1,1}=[]

    a =

    [] []
    [2x3 double] [1x3 double]

    >> a={20,'matlab';ones(2,3),1:3}
    
    a = 
    
        [        20]    'matlab'    
        [2x3 double]    [1x3 double]
    
    >> a(2)
    
    ans = 
    
        [2x3 double]
    
    >> a(3)
    
    ans = 
    
        'matlab'

    可以看出四个元胞数组的排序

    >> a{4}=[]
    
    a = 
    
        [        20]    'matlab'
        [2x3 double]          []
    
    >> a(4)=[]
    
    a = 
    
        [20]    [2x3 double]    'matlab'

    删除一个之后的重新排序。

      

  • 相关阅读:
    $Noip2018/Luogu5022$ 旅行
    $Noip2018/Luogu5020$ 货币系统 $dp$
    $Noip2018/Luogu5021$ 赛道修建 二分+树形
    $Noip2018/Luogu5019/Luogu1969$ 铺设道路
    $Poj1220/AcWing124 Number Base Convertion$ 进制转换+高精除
    $Poj1050 To the Max$
    $Poj1723/AcWing123 Soldiers$ 排序
    luogu质数距离
    模板线性筛
    CERC2016 bfs 最大生瓶颈边 lca
  • 原文地址:https://www.cnblogs.com/ruo-li-suo-yi/p/7648353.html
Copyright © 2020-2023  润新知