使用逻辑表达式建立索引对字符串有用吗? [英] Does Index Using a Logical Expression work for strings?

查看:84
本文介绍了使用逻辑表达式建立索引对字符串有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面

http://www.mathworks. com/help/matlab/matlab_prog/access-data-in-a-table.html

表明我们可以对表使用逻辑表达式.但它仅适用于数字列,不适用于字符串列.

shows that we can use logic expressions with tables. But it works only for numeric columns but not string columns.

>> T = table(categorical({'M';'F';'M'}),[45;32;34],...
             {'NY';'CA';'MA'},logical([1;0;0]),...
             'VariableNames',{'Gender' 'Age' 'State' 'Vote'})

T = 

Gender    Age    State    Vote 
______    ___    _____    _____

M         45     'NY'     true 
F         32     'CA'     false
M         34     'MA'     false

>> T.State == 'CA'
Undefined operator '==' for input arguments of type 'cell'.

>> T.Age == 32

ans =

 0
 1
 0

我们可以使它适用于字符串吗?

Can we make it work for strings?

推荐答案

使用strcmp比较单元格中的字符串:

Use strcmp for comparing strings in cells:

>> strcmp(T.State, 'CA')

ans =

  3×1 logical array

   0
   1
   0

这篇关于使用逻辑表达式建立索引对字符串有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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