Compare two strings, in different length, in two data sets in MATLAB, using if loop.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello Everyone,
I want to have compare two strings in two data sets. Data set 1 has two columns of data, the first column is in words, the second numbers. Data set 2 only has one column of data in words. The two strings I want to compare do not have same length, with String 1 having 178 rows and String 2 having 50 rows.
I want to compare the two strings like this: if String 1 is equal to String 2, for example,'CT'='CT', I want it to read and store the number in the corresponding row in the second column in Data set 1.For some words, they might have more than one number to match so I want to store all the numbers that corresponds to 'CT'='CT'. Else, go to the next one and compare again.
I hope that I made myself clear in explaining this. I would really appreciate it if anyone can help me out. Because I am a beginner in MATLAB, could you please explain more in detail so that I can understand better? Thank you so much for being willing to help me.
Sincerely, Kerry
2 Kommentare
Antworten (2)
John BG
am 20 Mär. 2016
Hi Mr Chen
1.- use cells If you work with strings only, you are going to be limited to constant length. If instead you work with cells of strings, you can generalize the any string length.
2.- From your question i understand you have a base of strings
str_base1={'awdv'};str_base2={'ft'};str_base3={'pd7'}
str_base={str_base1 str_base2 str_base3}
and a list of input strings
str1={'ft'};str2={'pd6'};str3={'aw32'};str4={'ft'};str5={'awdv'};str5={'09iy'};str6={'ft'}
str={str1 str2 str3 str4 str5 str6}
you want to know how many times each base is equal, exactly equal to any of the base strings. You could also want to 'sweep' each base string along each input string, but you clearly mention 'CT'='CT' so i do not consider matching base strings inside longer input strings.
3.- the core operation you are after is already implemented with command strcmp
length_base=length(str_base)
length_input=length(str)
match_log=[0 0]
for i=1:length_base
for j=1:length_input
if strcmp(str_base{i},str{j})
match_log=[match_log;i j]
end
end
end
match_log(1,:)=[]
match_log =
2.00 1.00
2.00 4.00
2.00 6.00
In this basic example, only the second base string shows up 3.
There is a lot more that can be done but if you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
Azzi Abdelmalek
am 20 Mär. 2016
v={'ct' 12;'er' 14 ;'ct' 20 ; 'gf' 45 ;'ct' 78 ; 'er' 47}
[ii,jj,kk]=unique(v(:,1))
out=[ii accumarray(kk,(1:numel(kk))',[],@(x) {[v{x,2}]})]
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!