Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 比较相同行的两个单元阵列-MATLAB_Performance_Matlab_Compare_Cells_Cell Array - Fatal编程技术网

Performance 比较相同行的两个单元阵列-MATLAB

Performance 比较相同行的两个单元阵列-MATLAB,performance,matlab,compare,cells,cell-array,Performance,Matlab,Compare,Cells,Cell Array,我有一个包含40000行的单元格字符串矩阵和一个包含400行的单元格字符串矩阵。我需要在第一个矩阵中找到适合第二个的行(行)。请注意,可能会有很多重复 看起来: 40000行 Anna Frank Anna George Jane Peter Anna George Jane Peter etc. 在这里,我需要找到适合的 Anna George Jane Peter 我发现far的唯一方法是两个for函数和介于两者之间的if。但这相当缓慢: for i=2:

我有一个包含
40000
行的单元格字符串矩阵和一个包含
400
行的单元格字符串矩阵。我需要在
第一个
矩阵中找到适合
第二个
的行(行)。请注意,可能会有很多重复

看起来:
40000

Anna Frank  
Anna George  
Jane Peter  
Anna George  
Jane Peter    
etc.
在这里,我需要找到适合的

Anna George  
Jane Peter  
我发现far的唯一方法是两个
for
函数和介于两者之间的
if
。但这相当缓慢:

for i=2:size(bigTable,1)
    for j = 1: size(smallTable,1)
        if sum(ismember(bigTable(i,1:2),smallTable(j,1:2))) == 2
            Total_R(size(Total_R,1)+1,1)= i;
        end
    end
end

我假设你的输入是这样设置的-

bigTable = 
    'Anna'    'Frank' 
    'Anna'    'George'
    'Jane'    'Peter' 
    'Anna'    'George'
    'Jane'    'Peter' 
smallTable = 
    'Anna'    'George'
    'Jane'    'Peter' 
为了解决您的问题,这里可以建议两种方法

方法#1

基于ismember的方法-

Total_R = find(sum(ismember(bigTable,smallTable,'rows'),2)==2)
方法#2

%// Assign unique labels to each cell for both small and big cell arrays, so that
%// later on you would be dealing with numeric arrays only and 
%// do not have to mess with cell arrays that were slowing you down
[unqbig,matches1,idx] = unique([bigTable(:) ; smallTable(:)])
big_labels = reshape(idx(1:numel(bigTable)),size(bigTable))
small_labels = reshape(idx(numel(bigTable)+1:end),size(smallTable))

%// Detect which rows from small_labels exactly match with those from big_labels
Total_R  = find(ismember(big_labels,small_labels,'rows'))
或者将最后一行的
ismember
替换为基于
bsxfun
的实现-

Total_R = find(any(all(bsxfun(@eq,big_labels,permute(small_labels,[3 2 1])),2),3))

假设输入情况下这些方法的输出-

Total_R =
     2
     3
     4
     5

你考虑过把名字和姓氏连在一起吗?注意:对于2010或最新版本的MATLAB,您可以跳过某些输出,因此您可以这样做-
[~,~,idx]=unique([bigTable(:);smallTable(:)])