comparing two strings with Database?

조회 수: 1 (최근 30일)
Isay
Isay 2014년 11월 6일
댓글: Guillaume 2014년 11월 6일
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