分享

MATLAB中三种数组之间的转换

 maoj66 2015-01-14

三种数组是:数值型矩阵,字符数组(或字符串),单元数组(也有叫元胞数组)

1、数值型矩阵转换为字符数组(num2str、mat2str)

>> A=[1,2,3;4,5,6];
>> B=num2str(A)

B =

3
6

>> whos
  Name      Size            Bytes  Class     Attributes

         2x3                48  double             
         2x7                28  char  

 

2、字符数组转换为数值型(str2num<4位精度>、str2double<双精度>)

>> B=['123.34'];
>> A=str2num(B)

A =

  123.3400

>> whos
  Name      Size            Bytes  Class     Attributes

         1x1                 double             
         1x6                12  char   

 

3、单元数组转换为字符数组(cell2mat):

例如:

>> A={'sfjsdlgksdgjlsdjgkl'};
>> B=cell2mat(A)

B =

sfjsdlgksdgjlsdjgkl

>> whos
  Name      Size            Bytes  Class    Attributes

         1x1                98  cell              
         1x19               38  char         

 

4、字符数组转换为单元数组(cellstr)

>> A=['what';'edit';'play'];%注意:用A=[]生成字符数组是,必须保证每行有相同的长度,不够可用空格补
>> B=cellstr(A)

B =

    'what'
    'edit'
    'play'

>> whos
  Name      Size            Bytes  Class    Attributes

         3x4                24  char              
         3x1               204  cell    

 

5、单元数组转换为数值型矩阵

>> A={'123.4'};
>> B=cell2mat(A)

B =

123.4

>> C=str2num(B)

C =

  123.4000

>> whos
  Name      Size            Bytes  Class     Attributes

         1x1                70  cell               
         1x5                10  char               
         1x1                 double 

 

6、数值型矩阵转换为单元数组(num2cell)

>> A=[1,2,3;4,5,6];
>> B=num2cell(A)

B =

    [1]    [2]    [3]
    [4]    [5]    [6]

>> whos
  Name      Size            Bytes  Class     Attributes

         2x3                48  double             
         2x3               408  cell  

 

 

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多