0

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.

1
  • 1
    Take a look at strfind, example: document={'trees reduce pollution','trees prevent floods'};index=strfind(document,'pollution');found=cellfun(@(x)(~isempty(x)),index) Commented Feb 24, 2015 at 8:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a ton, this logic worked for me absolutely fine.cheers
Thanks for your nice comment. Don't hesitate to mark this question as "solved".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.