【问题标题】:How to create a sub-cell from a cell array in Matlab?如何从 Matlab 中的单元格数组创建子单元格?
【发布时间】:2018-02-22 20:47:13
【问题描述】:

假设我在 Matlab 中有以下元胞数组数据:

>> data = {'first', 1; 'second', 2; 'third', 3}

data = 

    'first'     [1]
    'second'    [2]
    'third'     [3]

然后我想创建一个只有第一列数据的新元胞数组。我尝试了以下方法,但只得到了 first 值。

>> column_1 = data{:,1}

column_1 =

first

但我想得到的输出是:

>> column_1 = {'first';'second';'third'}

column_1 = 

    'first'
    'second'
    'third'

如何从data 单元格数组的第一列创建子单元格?

【问题讨论】:

    标签: matlab cell-array


    【解决方案1】:

    你必须使用圆括号索引而不是花括号索引,像这样:

    data(:,1)
    

    输出:

    ans =
          3×1 cell array
          'first'
          'second'
          'third'
    

    基本上,花括号的目的是检索单元格的底层内容并呈现不同的行为。要提取单元格子集,您需要使用圆括号。更多详情请参考 Matlab 官方文档的this page。

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2013-11-16
      • 2013-11-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多