我有一个像这样的单元格数组(截断):
'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”。我需要在第二个数组中找到每个状态代码,并将其匹配到第一个数组中给定的正确区域。然后,我需要按照区域代码对第二个数组进行排序。
我该怎么做?我不知道如何比较从第二个数组到第一个数组的第三列的前两个数字,并给它分配新的区域。一旦完成这些步骤,排序就不会太困难了。
发布于 2015-02-27 18:20:47
假设A和B分别是第一个和第二个数组,这将是一种方法-
%// 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,:)https://stackoverflow.com/questions/28771412
复制相似问题