Using loops and strcmp. Have 1 master char array, need to find matches of each entry in 2 other arrays.
Show older comments
I have 3 variables, each are list-like char arrays.
Var1 is a list of unique 3-letter strings, in alphabetical order.
Var2 and Var 3 are lists where the entries are a random combination of the unique strings in Var1. The unique strings can appear multiple times or not at all. Var 2 and 3 are of equal length, but not Var1.
What I have so far:
num_of_matches1=0;
num_of_matches2=0;
for k=1:length(charstring1)
for i=1:length(charstring2)
MatchChecker=strcmp(charstring1(k),
charstring2(i));
if MatchChecker==1
num_of_matches1=num_of_matches1+1;
end
end
for j=1:length(charstring3)
MatchChecker=strcmp(charstring1(k),
charstring3(j));
if MatchChecker==1
num_of_matches2=num_of_matches2+1;
end
end
Eventually, I need to print results in a table like this:
Charstring Name Matches in Var2 Matches in Var3
ABC 5 8
BAS 8 7
etc.
Accepted Answer
More Answers (1)
>> mtch=[cellfun(@(x) sum(ismember(C2,x)),C1) ...
cellfun(@(x) sum(ismember(C3,x)),C1)];
>> mtch=[mtch sum(mtch,2)]
mtch =
2 4 6
1 2 3
4 1 5
>>
NB:
I converted to cellstr before for brevity...
C1 = cellstr(Var1); % etc., ...
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!