【问题标题】:Compare and sort based on overlap根据重叠进行比较和排序
【发布时间】:2023-03-25 10:17:01
【问题描述】:

我有一个像这样(截断)的元胞数组:

'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”。我需要在第二个数组中找到每个状态代码,并将其与第一个数组中给出的正确区域相匹配。然后,我需要按区域代码对第二个数组进行排序。

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

【问题讨论】:

    标签: matlab sorting find compare


    【解决方案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,:)
    

    【讨论】:

    • 几乎我会做的,但我使用正则表达式代替大声笑。 +1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多