如何在给定特定条件的情况下将元素传递给不同的单元格数组

时间:2014-05-16 15:08:40

标签: arrays matlab string-matching similarity cell-array

我正在使用Matlab,我有两个不同的单元格数组,包含多个元素。

主要和次要的。虽然行数和顺序不相等,但在两个单元格中都有一个共同的元素。我希望每次验证条件时,来自辅助单元的X个额外元素“传递”到主要元素。如果列Y(来自主小区)和Z(来自辅助小区)匹配,则条件是。例如:

Primary cell array:

ABC 970508 …

FED 970524 …

BAC 970601 …

IGH 970606 …

Secondary cell array

IGH FINANCE BANK1

FED HEALTH PILLS

ABC FINANCE BANK3

What I would like to get in the ‘new’ primary cell array:

ABC 970508 FINANCE BANK3

FED 970524 HEALTH PILLS

BAC 970601 …

IGH 970606 FINANCE BANK1

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

如果您使用数据库,我相信您想要做的是内部联接。在Join Matrices in MATLAB中更全面地讨论了连接单元格数组,但是将它应用于您的特定情况,以下代码应该可以为您提供所需的内容:

d1 = {'ABC' 970508
    'FED' 970524
    'BAC' 970601
    'IGH' 970606};

d2 = {'IGH' 'FINANCE' 'BANK1'
    'FED' 'HEALTH' 'PILLS'
    'ABC' 'FINANCE' 'BANK3'};

%// get all possible keys, and convert them to indices starting at 1
[keys,~,ind] = unique( [d1(:,1);d2(:,1)] );

%// inner join
ind1 = ind(1:size(d1,1));
ind2 = ind(size(d1,1)+1:end);

loc1 = ismember(ind1, ind2);
loc2 = ismember(ind2, ind1);

innerJoin = cell(sum(loc1),3);
innerJoin(:,1) = d1(loc1,1);
innerJoin(:,2) = d1(loc1,2);        
innerJoin(:,3) = d2(loc2,2);
innerJoin(:,4) = d2(loc2,3);

这给出了

innerJoin = 

'ABC'    [970508]    'FINANCE'    'BANK1'
'FED'    [970524]    'HEALTH'     'PILLS'
'IGH'    [970606]    'FINANCE'    'BANK3'

最近版本的MATLAB似乎还有 table 类型,它具有原生 innerjoin 功能,因此您可以使用这些而不是单元格数组。见the Mathworks documentation。但是,这不是我的MATLAB版本,即2012a。

答案 1 :(得分:0)

您希望如何使用bsxfun

的矢量化解决方案

<强>代码

%%// a1 and a2 are primary and secondary cell arrays resepectively

a1 ={
'a' 'ABC is correct' '970508'
'bb' 'FED' '970524'
'dwd' 'BAC' '970601'
'hoi' 'IGH' '970606'}

a2 = {
'what' 'gap' 'IGH' 'FINANCE BANK1'
'nope' 'seal' 'FED' 'HEALTH PILLS'
'yes' 'solo' 'ABC is correct' 'FINANCE BANK3'}

X = 1;%// Number of extra columns to append from secondary cell array
Y = 2;%%// Column number from primary cell array to choose from
Z = 3;%%// Column number from secondary cell array to choose from

a1col = char(a1(:,Y))
a2col = char(a2(:,Z))

ad1 = size(a2col,2)-size(a1col,2)

a1col = [a1col repmat(' ',size(a1,1),ad1)]
a2col = [a2col repmat(' ',size(a2,1),-ad1)]

tt1 = a1col-'0'
tt2 = a2col-'0'

tt3 = permute(tt2,[3 2 1])
p1 = bsxfun(@eq,tt1,tt3)
p2 = squeeze(all(p1,2))

[v1,v2] = max(p2,[],2)

%// out is the desired output
out = cell(size(a1,1),size(a1,2)+X)
out(:,1:size(a1,2)) = a1
out(v1,:) = horzcat(a1(v1,:),a2(v2(v1),Z+1:Z+X))

<强>输出

a1 = 
    'a'      'ABC is correct'    '970508'
    'bb'     'FED'               '970524'
    'dwd'    'BAC'               '970601'
    'hoi'    'IGH'               '970606'

a2 = 
    'what'    'gap'     'IGH'               'FINANCE BANK1'
    'nope'    'seal'    'FED'               'HEALTH PILLS' 
    'yes'     'solo'    'ABC is correct'    'FINANCE BANK3'


out = 
    'a'      'ABC is correct'    '970508'    'FINANCE BANK3'
    'bb'     'FED'               '970524'    'HEALTH PILLS' 
    'dwd'    'BAC'               '970601'                 []
    'hoi'    'IGH'               '970606'    'FINANCE BANK1'