比较两个字符串数组

Compare two arrays of strings

我有两个字符串列表作为 table 中的一列(PM25_spr{i}.MonitorIDO3_spr{i}.MonitorID)。列表的长度不同。我想比较每个条目的前 11 个字符,并提取每个列表相同的索引。

示例

List 1:
    '01-003-0010-44201'
    '01-027-0001-44201'
    '01-051-0001-44201'
    '01-073-0023-44201'
    '01-073-1003-44201'
    '01-073-1005-44201'
    '01-073-1009-44201'
    '01-073-1010-44201'
    '01-073-2006-44201'
    '01-073-5002-44201'
    '01-073-5003-44201'
    '01-073-6002-44201'

List 2:
    '01-073-0023-88101'
    '01-073-2003-88101'
    '04-013-0019-88101'
    '04-013-9992-88101'
    '04-013-9997-88101'
    '05-119-0007-88101'
    '05-119-1008-88101'
    '06-019-0008-88101'
    '06-029-0014-88101'
    '06-037-0002-88101'
    '06-037-1103-88101'
    '06-037-4002-88101'
    '06-059-0001-88101'
    '06-065-8001-88101'
    '06-067-0010-88101'
    '06-073-0003-88101'
    '06-073-1002-88101'
    '06-073-1007-88101'
    '08-001-0006-88101'
    '08-031-0002-88101'

我试过 intersect,这不是我想做的事情的正确方法。我不确定如何使用 ismember,因为我只想查看前 11 个字符。

我尝试了 strncmp,但是 Inputs must be the same size or either one can be a scalar.

chars2compare = length('18-097-0083'); 
strncmp(O3_spr{i}.MonitorID, PM25_spr{i}.MonitorID,chars2compare)
PM25_spr_MID = cell(length(years),1); % Preallocate cell array
for n = 1:length(PM25_spr{i}.MonitorID) 
    s = char(PM25_spr{i}.MonitorID(n)); % Convert string to char
    PM25_spr_MID{i}(n) = cellstr(s(1:11)); % Pull out 1-11 characters and convert to cell
end

O3_spr_MID = cell(length(years),1); % Preallocate cell array
for n = 1:length(O3_spr{i}.MonitorID)
    s = char(O3_spr{i}.MonitorID(n));
    O3_spr_MID{i}(n) = cellstr(s(1:11));
end

[C, ia, ib] = intersect(O3_spr_MID{i}, PM25_spr_MID{i}) 
PerCap_spr_O3{i} = O3_spr{i}(ia,:);
PerCap_spr_PM25{i} = PM25_spr{i}(ib,:);

假设 list1list2 是两个输入元胞数组,您可以使用几种方法。

我。对元胞数组进行操作

intersect-

%// Clip off after first 11 characters in each cell of the input cell arrays
list1_f11 = arrayfun(@(n) list1{n}(1:11),1:numel(list1),'uni',0)
list2_f11 = arrayfun(@(n) list2{n}(1:11),1:numel(list2),'uni',0)

%// Use intersect to find common indices in the input cell arrays
[~,idx_list1,idx_list2] = intersect(list1_f11,list2_f11)

ismember-

%// Clip off after first 11 characters in each cell of the input cell arrays
list1_f11 = arrayfun(@(n) list1{n}(1:11),1:numel(list1),'uni',0)
list2_f11 = arrayfun(@(n) list2{n}(1:11),1:numel(list2),'uni',0)

%// Use ismember to find common indices in the input cell arrays
[LocA,LocB] = ismember(list1_f11,list2_f11);
idx_list1 = find(LocA)
idx_list2 = LocB(LocA)

二.对字符数组进行操作

我们可以直接在输入元胞数组上使用 char 来获得 2D 字符数组,因为使用它们可能比使用 cells.

更快

intersect+'rows'-

%// Convert to char arrays
list1c = char(list1)
list2c = char(list2)

%// Clip char arrays after first 11 columns
list1c_f11 = list1c(:,1:11)
list2c_f11 = list2c(:,1:11)

%// Use intersect with 'rows' option
[~,idx_list1,idx_list2] = intersect(list1c_f11,list2c_f11,'rows')

三。对数值数组进行操作

我们可以将 char 数组进一步转换为只有一列的数值数组,因为这可能会导致更快的解决方案。

%// Convert to char arrays
list1c = char(list1)
list2c = char(list2)

%// Clip char arrays after first 11 columns
list1c_f11 = list1c(:,1:11)
list2c_f11 = list2c(:,1:11)

%// Remove char columns of hyphens (3 and 7 for the given input)
list1c_f11(:,[3 7])=[];
list2c_f11(:,[3 7])=[];

%// Convert char arrays to numeric arrays
ncols = size(list1c_f11,2);
list1c_f11num = (list1c_f11 - '0')*(10.^(ncols-1:-1:0))'
list2c_f11num = (list2c_f11 - '0')*(10.^(ncols-1:-1:0))'

从这一点开始,您还可以使用下面列出的三种方法。

With ismember(内存效率高,但可能不是所有数据大小都快) -

[LocA,LocB] = ismember(list1c_f11num,list2c_f11num);
idx_list1 = find(LocA)
idx_list2 = LocB(LocA)

使用 intersect(可能会很慢) -

[~,idx_list1,idx_list2] = intersect(list1c_f11num,list2c_f11num)

With bsxfun(内存效率低,但对于小到体面的输入可能很快) -

[idx_list1,idx_list2] = find(bsxfun(@eq,list1c_f11num,list2c_f11num'))