matlab中strcmp函数的使用

您所在的位置:网站首页 matlab的功能及应用 matlab中strcmp函数的使用

matlab中strcmp函数的使用

2023-10-28 03:17| 来源: 网络整理| 查看: 265

strcmp是用于做字符串比较的函数,按复杂程度及比较对像的不同主要可以分为以下三种情况:

1. TF=strcmp(s1,s2);

    s1和s2是字符串,比如:s1=‘hello’,s2='matlab'。

    如果s1和s2是一致的(identical),则返回值TF=1,否则,TF=0。

    e.g.

    >> s1='hello';    >> s2='hello';    >> s3='matlab';    >> TF1=strcmp(s1,s2);    >> TF1    TF1 =         1    >> TF2=strcmp(s1,s3);    >> TF2    TF2 =         0

2. TF=strcmp(s,c);

    s是一个字符串,the c is a cell array,c的元素全是字符串,比如:s=‘hello’,c={'hello','matlab';'HELLO','matlab'}。

    返回值TF是一个和c有相同size的logical array,TF的元素是1或0。

    把c中的每个元素和s做比较,如果一致,则TF对应位置的元素为1,否则,为0。

    e.g.

    >> s='hello';    >> c={'hello','matlab';'HELLO','matlab'};    >> TF=strcmp(s,c);    >> TF    TF =     1     0     0     0

3. TF=strcmp(c1,c2);          

    c1和c2都是cell arrays,并且它们具有相同的size,它们的元素都是字符串,比如c1={'hello','matlab';'HELLO','matlab'};c2={'hello','matlab';'hello','MATLAB'};

    返回值TF是一个和c1或c2有相同size的logical array,TF的元素是1或0。

    把c1和c2对应位置的元素做比较,如果一致,则TF对应位置的元素为1,否则,为0。

    e.g.

    >> c1={'hello','matlab';'HELLO','matlab'};    >> c2={'hello','matlab';'hello','MATLAB'};    >> TF=strcmp(c1,c2);    >> TF    TF =     1     1     0     0

    

   

       



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3