根据重叠进行比较和排序

时间:2015-02-27 17:58:07

标签: matlab sorting find compare

我有一个看起来像这样的单元格(截断):

'State Name'                   'State Abbr'    'State Code'    'Region'
-----------------------------------------------------------------------
'Alabama'                      'AL'            '01'            '04'
'Alaska'                       'AK'            '02'            '10'
'Arizona'                      'AZ'            '04'            '09'
'Arkansas'                     'AR'            '05'            '06'
'California'                   'CA'            '06'            '09'
'Canada'                       'CC'            'CC'            '25'
'Colorado'                     'CO'            '08'            '08'
'Connecticut'                  'CT'            '09'            '01'
'Country Of Mexico'            'MX'            '80'            '25'
'Delaware'                     'DE'            '10'            '03'
'Delaware'                     'DE'            '10'            '03'
'Florida'                      'FL'            '12'            '04'
'Georgia'                      'GA'            '13'            '04'

我有另一个看起来像这样的数组(截断):

      MonitorID         POC    Latitude    Longitude     Datum           ParameterName           SampleDuration
___________________    ___    ________    _________    _______    __________________________    _______________

'01-073-0023-88101'    '1'    33.553      -86.815      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'01-073-0023-88101'    '1'    33.553      -86.815      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'01-073-0023-88101'    '1'    33.553      -86.815      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'01-073-0023-88101'    '1'    33.553      -86.815      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'01-073-0023-88101'    '1'    33.553      -86.815      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'
'02-170-0013-88101'    '1'    61.599      -149.46      'WGS84'    'PM2.5 - Local Conditions'    '24 HOUR'

我想要做的是按照第一个数组中的区域代码对第二个数组(实际上有更多的行和列)进行排序。

现在,第二个数组确实有区域代码。但是,它确实有州代码。状态代码是MonitorID列中的前两个数字。例如,对于'01 -073-0023-88101',状态代码为'01'。我需要在第二个数组中找到每个状态代码,并将其与第一个数组中给出的正确区域相匹配。然后,我需要按区域代码对第二个数组进行排序。

我该怎么做?我不确定如何将第二个数组中的前两个数字与第一个数组的第三列进行比较并为其分配新区域。一旦这些步骤完成,对它进行排序应该不会太困难。

1 个答案:

答案 0 :(得分:1)

假设AB分别是第一个和第二个数组,这将是一种方法 -

%// Split the first column of B with "-" as the delimiter
Bcol1_split = cellfun(@(x) strsplit(x,'-'),B(:,1),'Uni',0)

%// Extract the first split string which would be the state codes
Bcol1_first_string = cellfun(@(x) x{1},Bcol1_split,'Uni',0)

%// Detect IDs of matching state codes from from B to those in A 
[~,matched_ID] = ismember(Bcol1_first_string,A(:,3))

%// Use those IDs to get corresponding Region codes for each row of data in B
mapped_region_codes = A(matched_ID,4)

%// Sort the region codes to get the IDs based on which B is to be
%// row-indexed, which would be the final output
[~,sorted_mapped_IDs] =  sort(mapped_region_codes)
outB = B(sorted_mapped_IDs,:)