MATLAB:字符串的单元格数组的比较 [英] MATLAB: comparison of cell arrays of string

查看:332
本文介绍了MATLAB:字符串的单元格数组的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单元格的字符串数组,我想检查他们是否包含相同的字符串(他们不必是在相同的顺序,我们也不知道他们是否具有相同的长度)。

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).

例如:

a = {'2' '4' '1' '3'};
b = {'1' '2' '4' '3'};

a = {'2' '4' '1' '3' '5'};
b = {'1' '2' '4' '3'};

首先我想到了 strcmp 需要循环一个单元格内容并与其他单元格进行比较。我还通过使用类似于

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)

但是我们不知道他们是同一个长度(明显不等)。那么你如何以最高效的方式执行这个比较,而不写太多的if / else的情况。

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.

推荐答案

函数 SETXOR ,它将返回值不在两个单元阵列的交叉点。如果它返回一个空数组,则两个单元格数组包含相同的值:

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:

arraysAreEqual = isempty(setxor(a,b));


编辑:一些效果评估...

d根据 Amro列出的两种解决方案测试我的解决方案的速度(使用 ISMEMBER 和< a href =http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html =nofollow noreferrer> STRCMP / CELLFUN )。我首先创建了两个大单元格数组:

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:

a = cellstr(num2str((1:10000).'));  %'# A cell array with 10,000 strings
b = cellstr(num2str((1:10001).'));  %'# A cell array with 10,001 strings



接下来,我运行每个解决方案100次,平均执行时间。然后,我交换了 a b 并重新安排。以下是结果:

Next, I ran each solution 100 times over to get a mean execution time. Then, I swapped a and b and reran it. Here are the results:

    Method     |      Time     |  a and b swapped
---------------+---------------+------------------
Using SETXOR   |   0.0549 sec  |    0.0578 sec
Using ISMEMBER |   0.0856 sec  |    0.0426 sec
Using STRCMP   |       too long to bother ;)

请注意, SETXOR 解决方案具有始终如一的快速定时。 ISMEMBER 解决方案实际上会运行得更快,如果 a 具有不在 b 中的元素。这是由于短路 &&&&&< / code> 跳过计算的后半部分(因为我们已经知道 a b 不包含相同的值)。但是,如果 a 中的所有值也在 b 中,则 ISMEMBER 解决方案的速度明显较慢。

Notice that the SETXOR solution has consistently fast timing. The ISMEMBER solution will actually run slightly faster if a has elements that are not in b. This is due to the short-circuit && which skips the second half of the calculation (because we already know a and b do not contain the same values). However, if all of the values in a are also in b, the ISMEMBER solution is significantly slower.

这篇关于MATLAB:字符串的单元格数组的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆