字符串单元阵列 - 检查在Matlab元素 [英] Cell array of strings - check elements in Matlab

查看:450
本文介绍了字符串单元阵列 - 检查在Matlab元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab的,如果我有一个字符串单元阵列,我怎么能检查是否例如第3行第1列等于某个给定的字符串,例如'ABC'

例如, myArray的(3,1)=='ABC'给我一个错误:


  

未定义操作符'=='类型'细胞'的输入参数。


  
  

错误cw14(19行)结果
      如果myArray的(3,1)==ABC



解决方案

这是因为你需要使用{大括号}为了访问单元阵列的的内容

使用(普通支架)索引实际的细胞的,而你的情况包含字符串。此外,为了检查我建议使用的 STRCMP 或者 strfind

因此​​使用这个:

  STRCMP(myarray的{3,1},ABC)

检查<一href=\"http://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html?refresh=true\"相对=nofollow>这里关于索引的相关信息为电池阵列。

修改(以下评论)

使用逻辑 == ,以便找到字符串成一个单元阵列是不是安全的,因为使用该运营商分割字符串,每个字母形成比较它,而不是到 STRCMP 和喜欢,可以检查整个字符串。

考虑这个code,其中我把一些字符串到 myArray的

  myArray的= {'A​​''B''ABC''CBA''ABC'}。myArray的=    '一个'
    B
    'ABC'
    CBA
    'ABC'

如果我们运用 == 字符串这个单元阵列上进行如下:

  Check_31 = {myArray的3,1} =='ABC'Check_41 = {myArray的4,1} =='CB_

Matlab的返回的2个逻辑矢量:

  Check_31 =     1 1 1
Check_41 =     1 1 0

所以你看到的,字符_是不是在细胞{4,1}字符串present的最后一个元素。

现在,如果我们使用 STRCMP (全细胞阵列上;我们不需要索引特异性细胞检查一些字符串是否为present):

  Check_ABC = STRCMP(myarray的,'ABC')

我们还可以得到一个逻辑向量,但此时不是指形成电池内部的串的3个字母,而是指的是单元阵列本身是否ABC是present与否。结果是这样的:

  Check_ABC =     0
     0
     1
     0
     1

这是有道理的,因为'ABC'确实是在细胞{3,1}和$ P $ {psent 5,1}。

希望这是更清晰!

In Matlab, if I have a cell array of strings, how can I check if for example 3rd row and 1st column is equal to some given string, for example 'ABC'?

For example, myArray(3,1) == 'ABC' gives me an error:

Undefined operator '==' for input arguments of type 'cell'.

Error in cw14 (line 19)
if myArray(3,1) == 'ABC'

解决方案

That's because you need to use {curly braces} in order to access the content of the cell array.

Using (regular brackets) indexes the actual cell, which in your case contains a string. Moreover, in order to check for the presence of strings I recommend using strcmp or maybe strfind.

Therefore use this:

strcmp(myArray{3,1},'ABC')

Check here for infos about indexing into cell arrays.

EDIT (following comments)

Using logical == in order to find for strings into a cell array is not safe, because using this operator splits the strings and compares every letter forming it, as opposed to strcmp and the likes, that checks the whole string.

Consider this code, where I put some strings into myArray:

myArray = {'A' 'B' 'ABC' 'CBA' 'ABC'}.'

myArray = 

    'A'
    'B'
    'ABC'
    'CBA'
    'ABC'

If we apply == on this cell array of strings as follows:

Check_31 = myArray{3,1} == 'ABC'

Check_41 = myArray{4,1} == 'CB_'

Matlab returns those 2 logical vectors:

Check_31 =

     1     1     1


Check_41 =

     1     1     0

So as you see, the character "_" is not the last element of the string present in the cell {4,1}.

Now if we use strcmp (on the whole cell array; we don't need to index specific cells to check whether some string is present):

Check_ABC = strcmp(myArray,'ABC')

We also get a logical vector, but this time not referring to the 3 letters forming the string inside the cell, but rather referring to the cell array itself and whether 'ABC' is present or not. The result is this:

Check_ABC =

     0
     0
     1
     0
     1

Which makes sense, since 'ABC' is indeed present in cells {3,1} and {5,1}.

Hope that's clearer!

这篇关于字符串单元阵列 - 检查在Matlab元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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