MATLAB字符串处理

来源:互联网 发布:linux虚拟文件系统 编辑:程序博客网 时间:2024/04/29 08:54

{}元胞单元(cell)

()元素单元

若x为元胞cell型矩阵

x(m,n)表示查看元胞矩阵的某个元素,x{m,n}表示展开元胞矩阵某个元素

x为数值矩阵,则x(m,n)取矩阵的某个元素,如x=[1,2,3; 4,5,6],其里面的元素为数值,则查看x(1,3)=3,无元胞,无展开运算。

x为元胞矩阵,则x(m,n)取元胞的某个元素,如x={1,'a',3;4,'b',6},其里面的元素是元胞,则查看x(1,3)=[3]=3,x(1,2)='a',展开x{1,2}=a

把元胞矩阵x放入到元胞矩阵y的1行1列元胞位置

把元胞矩阵str放入到元胞矩阵y的2行1列元胞位置

>> x={1,'a',3;4,'b',6}

      y{1,1}=x

     str={'abc','dps','qq';'mm','nn','happy'}
     y{2,1}=str

y = 
    {2x3 cell}
    {2x3 cell}

查看元胞矩阵

>> y(1,1)
ans = 
    {2x3 cell}
>> y(2,1)
ans = 
    {2x3 cell}

展开元胞矩阵

>> y{1,1}
ans = 
    [1]    'a'    [3]
    [4]    'b'    [6]
>> y{2,1}
ans = 
    'abc'    'dps'    'qq'   
    'mm'     'nn'     'happy'

元胞矩阵y{2,1}里面的操作
>> y{2,1}(1,2)
ans = 
    'dps'

注意以下区别{}表示了展开,()表示元素

字符放入到字符单元

>> x(1,1)=str{1,1}
x =
a

字符放入到元胞单元

>> y{1,1}=str{1,1}

y = 
    'a'

元胞放入到字符单元

>> z(1,1)=str(1,1)

z = 
    'a'

元胞放入到元胞单元

>> h{1,1}=str(1,1)

h = 
    {1x1 cell}

>> h{1}
ans = 
    'a'

>> x(1,2)=str{2,1}
x =
ab

x为集合,则x(m,n)取集合矩阵里的某个集合,如x={1,'a',3;4,'b',6},则x{1,3}=3,x{1,2}=a

>> str
str = 
    {2x3 cell}
>> xx=str{1,1}
xx = 
    'abc'    'dps'    'qq'   
    'mm'     'nn'     'happy'
>> char(xx)
ans =
abc  
mm   
dps  
nn   
qq   
happy
>> ans(1,2)
ans =
b
字符串组成
>> x(1,1)='a'
x =
a
>> x(1,2)='b'
x =
ab
>> x(2,2)='c'
x =
ab
  c
>> x(2,3)='d'
x =
ab 
  cd

字符串集合组成

clear

>> x{1,1}='a'
x = 
    'a'
>> x{1,2}='b'
x = 
    'a'    'b'
>> x{2,2}='c'
x = 
    'a'    'b'
     []    'c'
>> x{2,3}='d'
x = 
    'a'    'b'     []
     []    'c'    'd'

创建一个集合

>> m={'123',[123 234]}

m = 

    '123'    [123 234]
>> m{1,1}
ans =
     123
>> m(1,1)
ans = 
    '123'
>> m{1,2}
ans =
   123 234
>> m(1,2)
ans = 
    [123 234]

字符串矩阵

>> str={'abc','dps','qq';'mm','nn','happy'}

str = 
    'abc'    'dps'    'qq'   

    'mm'     'nn'     'happy'

字符串矩阵行列数
>> [NP,D]=size(str)
NP =
     2

D =

     3

取第一行字符串组

>> [m,n,q]=str{1,:}
m =
abc
n =
dps

q =
qq

取第一行字符串组集

>> str(1,:)
ans = 
    'abc'    'dps'    'qq'

取第一个字符串

>> str1=str{1,1}
str1 =

abc

取第一个字符串集

>> str1=str(1,1)
str1 = 
    'abc'

取集中的字符串
>> str1{1}
ans =
abc

获取第1行第1列字符串长度
>> length(str{1,1})
ans =

     3

取第1行第1列字符串的第2个元素

>> str{1,1}(2)
ans =
b

str = 
    'abc'    'dps'    'qq'   
    'mm'     'nn'     'happy'
用nn代替str中的字符串
>> strrep(str,'nn','replace')
ans = 
    'abc'    'dps'        'qq'   
    'mm'     'replace'    'happy'

>> strrep(str{1,1},{'a','c'},{'t','m'})

ans = 

    'tbc'    'abm'

字符串

>> strg='hello,world'
strg =
hello,world
查找w字符的位置
>> findstr(strg,'w')
ans =
     7
>> strg(7)
ans =
w

查找字符o的位置

>> findstr(strg,'o')
ans =
     5     8

计算字符串中字符o的个数

>> size(findstr(strg,'o'),2)
ans =
     2

>> strg
strg =
hello,world
查找字符串lo的位置
>> findstr(strg,'lo')
ans =
     4

字符连接

>> strcat(strg(4),strg(5))
ans =
lo
strg =
hello,world
匹配‘,’
>> [a,b]=strtok(strg,',')
a =
hello

b=

,world

>> [a,b]=strtok(strg,'o')
a =
hell
b =
o,world

字符串组

mm = 

    'abc'    'dps'    'qq'    'd,df'
寻找d开头的字符串位置
>> strmatch('d',mm)
ans =
     2
     4

在字符串集合中取数组

str = 

    'a'    [1]

    'b'    [2]
>> num(1,1)=str{1,2};num(2,1)=str{2,2}
num =
     1
     2


一个决策树应用的字符串操作实例

%训练集
SampleMark={'咳嗽','头晕','体温','流感'}
Sample={
    '是','是','正常',  '否';....
    '是','是','高',    '是';....
    '是','是','非常高','是';....
    '否','是','正常',  '否';....
    '否','否','高',    '否';....
    '否','是','非常高','是';....
    '是','否','高',    '是';....
    '否','是','正常',  '否';....
    }


%流感为 是 的与 否 的两类子集
IsFlu=Sample(strmatch('是',Sample(:,4)),:)
NotFlu=Sample(strmatch('否',Sample(:,4)),:)


%计算期望信息I(N1,N2)
N1=size(IsFlu,1);
N2=size(NotFlu,1);
p1=N1/(N1+N2);
p2=N2/(N1+N2);


I=-p1*log2(p1)-p2*log2(p2)

。。。。。。。

原创粉丝点击