比较和查找二维数组中的字符串

时间:2022-12-03 15:57:08

I am New to Matlab and I am stuck in efficiently solving the following problem.

我是Matlab新手,一直在高效地解决下面的问题。

I have two arrays (both of them 2D) and I want to check array1 col by col to know how many elements appear in each col of array2 (compare col to col)

我有两个数组(都是2D的)我想检查array1 col by col看看array2的每个col有多少元素

for example

例如

array1

    ---------
    'a1'  'b1'    'c1'   
    'd1'  'e1'    'f1'

array2
----------
    'a1'   'a1'   'b1'  'b1'  'a1' 
    'd1'   'd1'   'c1'  'd1'  'c1'
    'c1'   'c1'   'b1'  'd1'  'd1'

I am trying to get the following output

我正在尝试获取以下输出

2 elements from array1 col1 appear in array2 col1
2 elements from array1 col1 appear in array2 col2
0 elements from array1 col1 appear in array2 col3
1 elements from array1 col1 appear in array2 col4
2 elements from array1 col1 appear in array2 col5

0 elements from array1 col2 appear in array2 col1
0 elements from array1 col2 appear in array2 col2
1 elements from array1 col2 appear in array2 col3
1 elements from array1 col2 appear in array2 col4
0 elements from array1 col2 appear in array2 col5

1 elements from array1 col3 appear in array2 col1
1 elements from array1 col3 appear in array2 col2
1 elements from array1 col3 appear in array2 col3
0 elements from array1 col3 appear in array2 col4
1 elements from array1 col3 appear in array2 col5
and so on

now I tried to do the below ugly code getting partial output

现在我试着做下面难看的代码来获得部分输出

for i=1:size(Array1,2)
   for m=1:size(Array1,1)
     element = Array1(i,m);

     indx =find(ismember(Array2,element));
     Array_match(indx) = Array_match(indx) + 1;
     end
    for s=2:size(Array1,1)
    if  length(char(Array1(s,i))) > 0 
        tt= tt + 1;
    end
    length(Array_test_words(s,i))
    end
    Indx2 = find((Array_match) > tt);
 end

2 个解决方案

#1


2  

Apply unique to the concatenation of both arrays to transform the cells into numeric labels. Remove repeated values in each column of the second array. Then test for equality with bsxfun and aggregate results for each combination of columns:

对两个数组的连接应用unique,将单元格转换为数字标签。删除第二个数组的每个列中的重复值。然后测试与bsxfun是否相等,并对每个列的组合进行汇总结果:

[~, ~, labels] = unique({array1{:} array2{:}});
a1 = reshape(labels(1:numel(array1)),size(array1)); %// array1 as numeric labels
a2 = reshape(labels(numel(array1)+1:end), size(array2)); %// same for array2
a2 = sort(a2);
a2(diff([NaN(1,size(a2,2)); a2])==0) = NaN; %// remove repeated values
m = bsxfun(@eq, permute(a1, [1 3 2]), permute(a2, [3 1 4 2])); %// find matches
result = squeeze(sum(reshape(m, [],size(a1,2),size(a2,2)), 1));

In your rexample this gives

在你的救赎中,这给予

result =
     2     2     0     1     2
     0     0     1     1     0
     1     1     1     0     1

#2


3  

Assuming that the input cell arrays have all the cells of single character strings, this might work for you -

假设输入单元格数组具有单个字符串的所有单元格,这可能对您有用

%// Input cell arrays
array1 = {
    'a1' 'b1' 'c1'   
    'd1' 'e1' 'f1'}

array2 = {
        'a1'   'a1'   'b1'  'b1'  'a1' 
    'd1'   'd1'   'c1'  'd1'  'c1'
    'c1'   'c1'   'b1'  'd1'  'd1'}

%// Convert input cell arrays to numeric arrays
array1n = char(array1)-0
array1n = reshape(array1n(:,1) * 1000 + array1n(:,2),size(array1))

array2n = char(array2)-0
array2n = reshape(array2n(:,1) * 1000 + array2n(:,2),size(array2))

out = squeeze(sum(any(bsxfun(@eq,array2n,permute(array1n,[3 4 1 2])),1),3))

Output -

输出-

out =
     2     0     1
     2     0     1
     0     1     1
     1     1     0
     2     0     1

#1


2  

Apply unique to the concatenation of both arrays to transform the cells into numeric labels. Remove repeated values in each column of the second array. Then test for equality with bsxfun and aggregate results for each combination of columns:

对两个数组的连接应用unique,将单元格转换为数字标签。删除第二个数组的每个列中的重复值。然后测试与bsxfun是否相等,并对每个列的组合进行汇总结果:

[~, ~, labels] = unique({array1{:} array2{:}});
a1 = reshape(labels(1:numel(array1)),size(array1)); %// array1 as numeric labels
a2 = reshape(labels(numel(array1)+1:end), size(array2)); %// same for array2
a2 = sort(a2);
a2(diff([NaN(1,size(a2,2)); a2])==0) = NaN; %// remove repeated values
m = bsxfun(@eq, permute(a1, [1 3 2]), permute(a2, [3 1 4 2])); %// find matches
result = squeeze(sum(reshape(m, [],size(a1,2),size(a2,2)), 1));

In your rexample this gives

在你的救赎中,这给予

result =
     2     2     0     1     2
     0     0     1     1     0
     1     1     1     0     1

#2


3  

Assuming that the input cell arrays have all the cells of single character strings, this might work for you -

假设输入单元格数组具有单个字符串的所有单元格,这可能对您有用

%// Input cell arrays
array1 = {
    'a1' 'b1' 'c1'   
    'd1' 'e1' 'f1'}

array2 = {
        'a1'   'a1'   'b1'  'b1'  'a1' 
    'd1'   'd1'   'c1'  'd1'  'c1'
    'c1'   'c1'   'b1'  'd1'  'd1'}

%// Convert input cell arrays to numeric arrays
array1n = char(array1)-0
array1n = reshape(array1n(:,1) * 1000 + array1n(:,2),size(array1))

array2n = char(array2)-0
array2n = reshape(array2n(:,1) * 1000 + array2n(:,2),size(array2))

out = squeeze(sum(any(bsxfun(@eq,array2n,permute(array1n,[3 4 1 2])),1),3))

Output -

输出-

out =
     2     0     1
     2     0     1
     0     1     1
     1     1     0
     2     0     1