comparing two strings with Database?
古いコメントを表示
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?
採用された回答
その他の回答 (1 件)
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
2014 年 11 月 6 日
Isay
2014 年 11 月 6 日
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?
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!