简体   繁体   中英

Matlab, find common elements of two cell arrays

I have two cell arrays, the sizes are 1x20033 and 1x19. Let's call these two cell arrays as A and B. I want to compare each cell of A with each cell of B to see if there is any common element.

Finally, I need to build a binary matrix and put one when there is a match.

I tried this:

BinaryMatrix=zeros(20033,19);

for i=1:1:20033

    for j=1:1:19 
        match=find(ismember(A{i},B{j}));        
        if match==1  
            BinaryMatrix(i,j)= 1;        
         end
    end
end

but I faced this error: "Input A of class double and input B of class cell must be cell arrays of strings, unless one is a string."

Please tell me What should I do to solve it?

The code that you have almost works. What I would recommend you do is split up the strings found in A and B by spaces. As such, A and B would then be cell arrays of elements where each element in A or B is a single word . The spaces will serve as delimiters for separating out the words.

Once you do this, use intersect to see if there are any common words between the words in A and the words in B . intersect works by considering two arrays (these can be numeric arrays, cell arrays, etc.) C and D as sets, and it returns the set intersection between these two arrays.

In our case, C and D would be a cell array of words separated by spaces from A and B . intersect(C,D) will return a cell array of strings where each element in the output is a string found in both C and D . As such, should this cell array be non-empty, we have found at least one common word between C and D . If this is the case, then set your binary flag at the location of your matrix to 1. In other words:

BinaryMatrix = false(20033,19);

for i=1:1:20033
    for j=1:1:19 
        Asplit = strsplit(A{i});
        Bsplit = strsplit(B{j});
        if (~isempty(intersect(Asplit, Bsplit)))
            BinaryMatrix(i,j)= true;        
        end
    end
end

You'll notice that I have changed your matrix from zeros(20033,19) , to false(20033,19) . The reason why is because by doing zeros , you are allocating 8 bytes per number in your matrix as this will create your matrix in double precision. By doing false , this will be a logical matrix instead, and you are allocating 1 byte per number. Seeing as how you want BinaryMatrix to be either true or false , don't use double - use logical . I don't know how large both cell arrays are, and so doing this will cut down your memory consumption by 8.

Minor Note

strsplit is only available from R2013a and onwards. If you have a version of MATLAB that is R2012b and lower, replace strsplit with regexp . As such, you would replace the two lines in the for loop with:

Asplit = regexp(A{i}, ' ', 'split');
Bsplit = regexp(B{j}, ' ', 'split');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM