在较大的数组中查找小数组中的单个字符串

时间:2012-07-03 10:27:42

标签: arrays string matlab

我在一个正在进行的项目中遇到了问题,希望Matlab可以为我节省无数个小时 我有一个数组包含字符串,旁边的列中有相应的整数(管道ID和长度)。我有大约7000个具有相应长度的管道ID。

如果我有200个管道的ID作为数组中的字符串,那么是否可以在大数组中找到这200个单独的字符串并让MATLAB给出位于单元格旁边的相应管道长度(整数) ID字符串?

我尝试使用ismemberstrmatch,但我无法让它发挥作用。

修改
包含所有数据的大型数组如下所示:

No.      ID                 Length (m)    
1        0-T103-0-T110      52.327    
2        0-T104-0-1370      30.4    
3        0-T104-0-1410      62.423    
4        0-T105-0-T109      46.611    
...    
7118     0415B-99878        152.242

然后我会以与上面相同的形式拥有一个较小的数组,但不是7118行,而是我将拥有200个。

到目前为止我尝试过:

a = {'0-T103-0-T110', 52.327; '0-T104-0-1370', 30.4; '0-T104-0-1410', 62.423};
ismember(a(:,1), '0-T104-0-1370');
leng = a{ismember(a(:, 1), '0-T104-0-1370'), 2}

这样可行,但正如您所看到的那样,它仅用于在小数组中定位单个字符串。

我已经加载了大型数组:

[num, text, raw] = xlsread('Pipelength_ALL.xlsx', 'A1:C7115');

1 个答案:

答案 0 :(得分:3)

您想使用ISMEMBER。这是一个例子:

%# list of all pipe ID's and their length (100 in this example)
pipes = [cellstr(num2str((1:100)','pip%03d')) num2cell(randi(50,[100 1]))];

%# pipeID's of the query (10 here)
x = randperm(100);
p = cellstr(num2str(x(1:10)','pip%03d'));

%# find matching lengths
[~,loc] = ismember(p, pipes(:,1));
len = cell2mat(pipes(loc,2));

结果样本:

>> [p num2cell(len)]
ans = 
    'pip096'    [14]
    'pip043'    [ 9]
    'pip095'    [27]
    'pip074'    [ 6]
    'pip001'    [ 3]
    'pip065'    [20]
    'pip067'    [ 8]
    'pip060'    [23]
    'pip051'    [27]
    'pip020'    [31]

编辑:

使用您发布的数据,代码为:

pipes = {
    '0-T103-0-T110'      52.327
    '0-T104-0-1370'      30.4
    '0-T104-0-1410'      62.423
    '0-T105-0-T109'      46.611
}

p = {
    '0-T104-0-1370'
    '0-T105-0-T109'
}

[~,loc] = ismember(p, pipes(:,1));
[p pipes(loc,2)]