Comparing strings in cell arrays of different sizes *Most efficient*
5 views (last 30 days)
Show older comments
Hi,
I have got two cell arrays, actually I got 3. example (excuse any typos im writing this off the top of my head, i just want to know the logics behind it)
Cellarray_1 = { 'a'; 'access'; 'also'; 'always'; 'and'; 'arrived' };
Cellarray_2 = [ 1 23 0 10 13 34 2 4 5 1 1];
Cellarray_3 = { 'a'; 'about'; 'above' ;'access'; 'addition'; 'also'; 'alter'; 'always'; 'akin' ; 'and'; 'arrived'};
The aim is to check which strings in Cellarray_1 are in Cellarray_3. From this find the index values to get the relevant element in Cellarray_2.
so the output should be a vector:
output = [1 10 34 4 1 1]
This is achieved because ("a", "access", "also", "always", "and", "arrived") appear in Cellarray_3 and there corresponding index values point to Cellarray_2 index values getting the data
I have got this so far:
for i=1:numel(Cellarray_1)
indexfind = strcmp(Cellarray_3,Cellarray_1(i));
testoutput(i) = Cellarray_2 (find(indexfind == 1))
end
Is there a more efficient way. As far as I can tell (in my tired state) there is but, I was hoping you Pros can guide me in the right direction.
Thanks in advance.
0 Comments
Accepted Answer
Teja Muppirala
on 10 Dec 2012
How about using the ISMEMBER function?
Cellarray_2(ismember(Cellarray_3,Cellarray_1))
ans =
1 10 34 4 1 1
More Answers (0)
See Also
Categories
Find more on Cell Arrays 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!