Compare two strings, in different length, in two data sets in MATLAB, using if loop.

2 Ansichten (letzte 30 Tage)
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
K Chen
K Chen am 20 Mär. 2016
Azzi, thank you so much for the suggestion. I attached two files to explain. In dataset1 and dataset2, I want to compare the first column at first. When the word in column 1 in dataset1 matches the word in dataset2, I want to get the corresponding numbers in dataset1 to multiply the corresponding number in column 2 in dataset2. I want to use it in the if loop because the data I have are huge. Thank you so much for the help and I hope to hear from you soon.
Sincerely, Kerry

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John BG
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
  1 Kommentar
K Chen
K Chen am 20 Mär. 2016
Hello John,
Thank you so much for your quick response and detailed explanation. Can I ask you a few questions, please? I don't want to count how many times that occurs. But rather, I want to know in which place the match occurs and I want to take the same row but the second column in data set 1. For example, in data set 1, I have number 12 and 15 in the second column in data set 1 to match 'CT'='CT'. I want to store those two numbers and then multiply them by another fixed number respectively. Right now, I am still stuck on locating the values.
Moreover, because dataset automatically uses the name of each workspace variable. I have been trying to turn it off but still cannot find a way to do it. Maybe that is also something giving me problems. Do you have any idea how to turn it off?
I hope I made myself clear about my questions. Thank you so much again for the great help and I hope to hear from you soon.
Sincerely, Kerry

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
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}]})]

Community Treasure Hunt

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

Start Hunting!

Translated by