Comparing lists of strings
Show older comments
OK so I have two lists of, say, names, List_one and List_two.
- List_one is smaller than List_two: they are both cell arrays.
I want to find the indices where List_two contains elements from List_one. So far, I've been using strcmpi, but because the lists are different sizes (and in different orders) I need to do it element by element, which I can't believe is the most efficient solution. Any tips?
Just now I'm doing something like:
for i = 1:length(List_one) tf = strcmpi(List_one(i),List_two); ind = ind + tf; % so that the end result List_two(ind) = List_one. end
and I just can't imagine that's the best way, though I've spent a long time reading about ways to compare lists, I still haven't found anything satisfactory. One more, related, question:
If I have a list of names, and someone permutes it (they don't give me the indices for the new ordering, only the new list) - what is the best way to uncover the indices?
As in, List_two(indices) = List_two_permuted; - how do I find the permutation?
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 17 Nov 2012
Edited: Azzi Abdelmalek
on 17 Nov 2012
List_one='is smaller than'
List_two='they are both cell arrays'
idx=find(ismember(List_one,List_two))
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!