comparing two strings with Database?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Isay
am 6 Nov. 2014
Kommentiert: Guillaume
am 6 Nov. 2014
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?
0 Kommentare
Akzeptierte Antwort
Titus Edelhofer
am 6 Nov. 2014
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
0 Kommentare
Weitere Antworten (1)
Guillaume
am 6 Nov. 2014
Bearbeitet: Guillaume
am 6 Nov. 2014
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 Kommentare
Guillaume
am 6 Nov. 2014
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?
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!