comparing two strings with Database?

1 次查看(过去 30 天)
Hi.
I have a DataBase(DB1) and i need to find "family" AND "work" in it.(Not OR).
my code is :
DB1 = {'family','city','May','30','(AFP)','work','US','prosecutors',... 'on','Friday','unveiled','a','14-count','indictment',... 'including','charges','of','murder','and','loan','sharking','against',... 'body','demands'};
count=0;
for p=1:size((DB1))
if strcmpi(DB1{p}, 'family') && strcmpi(DB1{p}, 'work')
fprintf('\n Found ')
count=count+1;
copyfile(file_name,des_file_addr)
end
end
p=p+1;
fprintf('\n count= %g',count)
fprintf('\n -----------------------' )
My code doesn't show True Result(it shows "count =0" , But True Result("count") must be 1), can anyone help me?

采纳的回答

Titus Edelhofer
Titus Edelhofer 2014-11-6
Hi,
just guessing: your DB1 is one row of your database? And you are looking for rows, in which you find both an occurrence of work and family? In this case I guess you would write
match = all(ismember({'work', 'family'}, DB1));
And if it's indeed only an example, and your DB1 would be a NxM cell array, the match would be
for i=1:size(p,1)
match_i = all(ismember({'work', 'family'}, DB1(p,:)));
end
Titus

更多回答(1 个)

Guillaume
Guillaume 2014-11-6
编辑:Guillaume 2014-11-6
Of course, in your loop you're asking whether each element of the cell array, a single string, is equal to 'family' and 'work'. A string can never be equal to two different things at the same time.
I'm not sure what exactly you mean to do. It's a bit odd to see a sentence broken up into individual words. Did you mean you wanted to find which sentences contain both words? Did you break the sentence up just to perform the search or for another purpose?
By the way, not that this would help in this case, but you can directly compare a cell array with a string. No need for a loop. Your code is equivalent to:
matches = strcmpi(DB1, 'family') & strcmpi(DB1, 'work'); %Of course will never match both.
count = sum(matches); %of course will always be 0.
  3 个评论
Isay
Isay 2014-11-6
can you help me more? youre code didn't help me:(
Guillaume
Guillaume 2014-11-6
Of course, my code didn't help. I just put it there to show you a simpler way of doing exactly what you did. As what you did doesn't work, the replacement doesn't either.
Despite your statement to the contrary, DB1 is obviously not your database since it's only just one sentence. So what is your database? A cell array of cell array? A true database from which you fetch one row at a time as a cell array? Something else?
If it's a true database from which you fetch one row at a time as a cell array, why doesn't Titus answer work for you?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by