【问题标题】:Creating nested or hierarchically indexed tables in Matlab r2017b在 Matlab r2017b 中创建嵌套或分层索引表
【发布时间】:2019-03-05 06:32:48
【问题描述】:

我想在 Matlab R2017b 中创建一个带有分层索引的表。

它看起来像 Python 中带有分层列的 pandas 数据框,例如:

                 bar                 baz                 foo                 qux          
          one       three       one       two       one       seven       one       two
A       0.895717  0.805244 -1.206412  2.565646  1.431256  1.340309 -1.170299 -0.226169
B       0.410835  0.813850  0.132003 -0.827317 -0.076467 -1.187678  1.130127 -1.436737
C      -1.413681  1.607920  1.024180  0.569605  0.875906 -2.211372  0.974466 -2.006747

这可能吗?如果是这样,我将如何访问与说 baz,2 关联的列?

【问题讨论】:

  • table 的每一列都可以有多个“列”,但这些列没有名称。请参阅示例here,其中“血压”列有两列数据。
  • 好的。但在我的情况下,我需要明确的列标签来显示血压列下的舒张压和收缩压值。
  • 改用结构体...或坚定地确定为什么您要尝试这样排列数据以查看是否有更好的方法-这种层次结构可能会性能较差,代码更丑

标签: matlab dataframe


【解决方案1】:

您可以在 R2017b 中制作嵌套表,如下所示:

t = table(table(rand(3,1), rand(3,1), 'VariableNames', {'one', 'three'}), ...
    table(rand(3,1), rand(3,1), 'VariableNames', {'one', 'two'}), ...
    'VariableNames', {'bar', 'baz'}, 'RowNames', {'A', 'B', 'C'})

在 R2017b 中,显示效果并不出色,如下所示:

t =
  3×2 table
             bar            baz    
         ___________    ___________
    A    [1x2 table]    [1x2 table]
    B    [1x2 table]    [1x2 table]
    C    [1x2 table]    [1x2 table]

但在 R2018b 中会更好:

t =
  3×2 table
                bar                   baz        
           one       three       one        two  
         __________________    __________________
    A    0.81472    0.91338     0.2785    0.96489
    B    0.90579    0.63236    0.54688    0.15761
    C    0.12699    0.09754    0.95751    0.97059

在任一版本中,您都可以使用t.bar.one 等访问嵌套表变量。

【讨论】:

    猜你喜欢
    • 2014-06-06
    • 2016-03-29
    • 1970-01-01
    • 2012-04-01
    • 2018-04-25
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多