在Matlab中连接来自不同单元格阵列的数据 [英] Joining data from different cell arrays in Matlab

查看:71
本文介绍了在Matlab中连接来自不同单元格阵列的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有单元格数组格式的数据,其中的列表示不同的项.单元格数组具有不同的列,如以下示例所示:

I have data in Matlab that is in cell array format with columns representing different items. The cell arrays have different columns, as in the following example:

a = {'A', 'B', 'C' ; 1, 1, 1; 2, 2, 2 }

a =

'A'    'B'    'C'
[1]    [1]    [1]
[2]    [2]    [2]

b = {'C', 'D'; 3, 3; 4, 4}

b =

'C'    'D'
[3]    [3]
[4]    [4]

我希望能够通过以下方式加入不同的单元格阵列:

I would like to be able to join the different cell arrays in the following manner:

c =

'A'    'B'    'C'    'D'
[1]    [1]    [1]    [NaN]
[2]    [2]    [2]    [NaN]
[NaN]  [NaN]  [3]    [3]
[NaN]  [NaN]  [4]    [4]

在实际示例中,我有数百列和几行,因此手动创建新的单元格阵列不是我的选择.

In the real example I have hundreds of columns and several rows, so creating a new cell array manually is not an option for me.

推荐答案

如果您愿意将数据存储在数据集数组中(或为此目的将其转换为数据集数组),则可以执行以下操作:

If you were willing to store your data in dataset arrays (or convert them to dataset arrays for this purpose), you could do the following:

>> d1
d1 = 
    A    B    C
    1    1    1
    2    2    2
>> d2
d2 = 
    C    D
    3    3
    4    4
>> join(d1,d2,'Keys','C','type','outer','mergekeys',true)
ans = 
    A      B      C    D  
      1      1    1    NaN
      2      2    2    NaN
    NaN    NaN    3      3
    NaN    NaN    4      4

这篇关于在Matlab中连接来自不同单元格阵列的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆