问题描述
如果我使用char字符串作为指针,而使用chars的单元格数组作为haysack,则以下内容每次 都能获得相同的结果吗?我正在查看他们的文档,但没有发现其他建议.我也想向SO的社区查询.
If I'm using a char string as the needle and a cell array of chars as the haysack, would the following achieve the same results every time? I'm looking at their documentations, but I don't see anything that would suggest otherwise. I wanted to check with SO's community as well.
基本上
k = strmatch('abc', cellArray, 'exact');
k2 = find(strcmp('abc', cellArray));
其中,cellArray
是char的Nx1单元数组,并且在任意索引处均具有'abc'值.例如,如果cellArray
在索引10、20和30处具有abc
.那么对于任何cellArray
,每次都为真?
where cellArray
is an Nx1 cell array of chars and it has 'abc' values at arbitrary indices. For example, if cellArray
has abc
at indices 10, 20, and 30. Then would the following be true everytime for any cellArray
?
k = [10 20 30];
k2 = [10 20 30];
此外,如果两种方法都返回相同的答案,那么在这种搜索情况下(何时在相同数据类型的单元格数组中查找char字符串),您将何时在strcmp
上使用strmatch
? strmatch
非常慢,如果有人想知道我为什么要问.
Also, if both methods return the same answers, when would you use strmatch
over strcmp
in this kind of search scenario (looking for a char string in a cell array of same data type)? strmatch
is extremely slow, if anyone is wondering why I'm even asking.
推荐答案
strmatch
.使用strncmp
或validatestring
代替. strmatch
将从Matlab的未来版本中删除.*警告在Matlab 2017 a
中给出.
strmatch
is not recommended. Use strncmp
or validatestring
instead. strmatch
will be removed from a future version of matlab. *Warning given in Matlab 2017 a
.
这篇关于MATLAB:strmatch与strcmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!