I have two cell arrays of strings, and I want to check if they contain the same strings (they do not have to be in the same order, nor do we know if they are of the same lengths).
For example:
a = {'2' '4' '1' '3'};
b = {'1' '2' '4' '3'};
or
a = {'2' '4' '1' '3' '5'};
b = {'1' '2' '4' '3'};
First I thought of strcmp
but it would require looping over one cell contents and compare against the other. I also considered ismember
by using something like:
ismember(a,b) & ismember(b,a)
but then we don't know in advance that they are of the same length (obvious case of unequal). So how would you perform this comparison in the most efficient way without writing too many cases of if/else.
You could use the function SETXOR, which will return the values that are not in the intersection of the two cell arrays. If it returns an empty array, then the two cell arrays contain the same values:
EDIT: Some performance measures...
Since you were curious about performance measures, I thought I'd test the speed of my solution against the two solutions listed by Amro (which use ISMEMBER and STRCMP/CELLFUN). I first created two large cell arrays:
Next, I ran each solution 100 times over to get a mean execution time. Then, I swapped
a
andb
and reran it. Here are the results:Notice that the SETXOR solution has consistently fast timing. The ISMEMBER solution will actually run slightly faster if
a
has elements that are not inb
. This is due to the short-circuit&&
which skips the second half of the calculation (because we already knowa
andb
do not contain the same values). However, if all of the values ina
are also inb
, the ISMEMBER solution is significantly slower.You can still use ISMEMBER function like you did with a small modification:
Also, you can write the loop version with STRCMP as one line:
EDIT: I'm adding a third solution adapted from another SO question:
In the same spirit, Im performed the time comparison (using the TIMEIT function):
and the results in the same order of functions (lower is better):
Take a look at the function
intersect
What MATLAB Help says: