Afzaal Ahmad
Afzaal Ahmad

Reputation: 197

comparing cell of strings with different size in matlab

i want to compare [1*232] cells of strings containing individual words from text document with [1*23] cells that contain individual sentences from the same text, can any one help me how to program it in Matlab? for example: "pollution" and "trees" are two words in separate cells and following are the two sentences in separate cells: 1. trees reduce pollution. 2. trees prevent floods.

what i want to do is put 0 or 1 after comparing pollution and trees with both the sentences or in my case "n" sentences and put 1's and 0's in the form of matrices. any help will be appreciated.

Upvotes: 0

Views: 82

Answers (1)

LowPolyCorgi
LowPolyCorgi

Reputation: 5171

You can use a combination of cellfun and strfind. Here is a try:

Sentences = {'trees reduce pollution' ; ... 
    'trees prevent floods' ; ...
    'pollution is bad' ; ...
    'flood is worse'};

Words = {'trees', 'pollution', 'bad'};

Out = NaN(numel(Sentences), numel(Words));

for i = 1:numel(Words)
    Out(:,i) = cellfun(@(x) numel(strfind(x, Words{i})), Sentences);
end

And Out contains:

Out =

     1     1     0
     1     0     0
     0     1     1
     0     0     0

Hope this helps.

Upvotes: 1

Related Questions