MATLAB: strmatch vs strcmp

后端 未结 2 727

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 d

相关标签:
2条回答
  • 2021-01-14 13:53

    No, the results will be different. The function strmatch returns a vector of indexes where the cell array (haystack) matches the string (needle):

    >> arr = {'a', 'b', 'c', 'a', 'b'};
    >> strmatch('a', arr, 'exact')
    ans =
        1
        4
    

    The strcmp function returns a logical vector, with 1s where the haystack matches and 0s where it doesn't match:

    >> strcmp('a', arr)
    ans =
        1   0   0   1   0
    

    On the other hand, the expression find(strcmp('a', arr)) is equivalent to strmatch('a', arr, 'exact').

    0 讨论(0)
  • 2021-01-14 13:57

    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.

    0 讨论(0)
提交回复
热议问题