MATLAB:合并两个相似的文本字段

时间:2015-09-08 13:59:47

标签: matlab matrix

我有两个类似文本字段,县和州的数组。我需要根据常见的县和州信息合并这两个数组。这里显示了一个简单的例子:

A = [Alabama    Chambers    36571
     Alabama    Cherokee    24006
     Alabama    Cherokee    24006
     Alabama    Jackson     53979
     Alabama    Jefferson   662033]

B = [2397530 -85.448 34.202 -86 35  Cherokee  Alabama
     2423130 -86.599 33.622 -86 33  Jefferson Alabama
     2423398 -86.576 33.524 -86 33  Jefferson Alabama
     2423496 -86.784 33.369 -86 33  Jefferson Alabama
     2455980 -86.696 33.711 -86 33  Jefferson Alabama
     2457595 -86.868 33.597 -86 33  Jefferson Alabama
     2458450 -86.879 33.518 -86 33  Jefferson Alabama];

产生的第三个矩阵:

C = [2397530 -85.448 34.202 -86 35  Cherokee Alabama  24006
     2423130 -86.599 33.622 -86 33  Jefferson Alabama 662033
     2423398 -86.576 33.524 -86 33  Jefferson Alabama 662033
     2423496 -86.784 33.369 -86 33  Jefferson Alabama 662033
     2455980 -86.696 33.711 -86 33  Jefferson Alabama 662033
     2457595 -86.868 33.597 -86 33  Jefferson Alabama 662033
     2458450 -86.879 33.518 -86 33  Jefferson Alabama 662033];

2 个答案:

答案 0 :(得分:0)

对于这个答案,我假设变量'A'和'B'都是单元格数组,并且与初始问题中给出的那些匹配。

假设您知道状态/县信息始终位于同一列(“A”为1& 2,“B”为1& 6),您可以使用以下代码循环显示“A” ,与'B'中的州/县信息进行比较,然后将相关信息附加到'B'(似乎需要的是将'A'的第3列附加到'B'的匹配行的末尾)。

stateIdxA=1;
countyIdxA=2;
stateIdxB=7;
countyIdxB=6;
appendIdxA=size(A,2); %We want to append the last element from A
appendIdxB=size(B,2)+1; % We want to append it to the end of B
for m = 1:size(B,1)
    jointStateCountyB{m} = [B{m,stateIdxB},B{m,countyIdxB}];
end

C=B; %Setup third cell array C to append to

for n = 1:size(A,1)
    jointStateCountyA = [A{n,stateIdxA},A{n,countyIdxA}];
    isSame = strcmp(jointStateCountyA,jointStateCountyB);
    C(isSame,appendIdxB) = A(n,appendIdxA);
end

答案 1 :(得分:0)

你可以使用带表格的单行代码来完成。

我假设你的数据是在像这样的单元格数组中:

A = { 'Alabama', 'Chambers',  36571
      'Alabama', 'Cherokee',  24006
      'Alabama', 'Jackson',   53979
      'Alabama', 'Jefferson', 662033 };

B = { 2397530, -85.448, 34.202, -86, 35,  'Cherokee',  'Alabama'
      2423130, -86.599, 33.622, -86, 33,  'Jefferson', 'Alabama'
      2423398, -86.576, 33.524, -86, 33,  'Jefferson', 'Alabama'
      2423496, -86.784, 33.369, -86, 33,  'Jefferson', 'Alabama'
      2455980, -86.696, 33.711, -86, 33,  'Jefferson', 'Alabama'
      2457595, -86.868, 33.597, -86, 33,  'Jefferson', 'Alabama'
      2458450, -86.879, 33.518, -86, 33,  'Jefferson', 'Alabama' };

然后您需要做的就是:

C = table2cell(innerjoin(cell2table(A), cell2table(B), 'LeftKeys', [1, 2], 'RightKeys', [7, 6]));

产生这个:

C = 

    'Alabama'    'Cherokee'     [ 24006]    [2397530]    [-85.4480]    [34.2020]    [-86]    [35]
    'Alabama'    'Jefferson'    [662033]    [2423130]    [-86.5990]    [33.6220]    [-86]    [33]
    'Alabama'    'Jefferson'    [662033]    [2423398]    [-86.5760]    [33.5240]    [-86]    [33]
    'Alabama'    'Jefferson'    [662033]    [2423496]    [-86.7840]    [33.3690]    [-86]    [33]
    'Alabama'    'Jefferson'    [662033]    [2455980]    [-86.6960]    [33.7110]    [-86]    [33]
    'Alabama'    'Jefferson'    [662033]    [2457595]    [-86.8680]    [33.5970]    [-86]    [33]
    'Alabama'    'Jefferson'    [662033]    [2458450]    [-86.8790]    [33.5180]    [-86]    [33]