和我一起练习MATLAB吧02——数据类型篇

数据结构

打开MATLAB和我一起尝试一下

命令行窗口输入

edit

显示编辑器窗口

在编辑器窗口输入

a1=int8(6)

a2=int16(-20)

a3=uint32(100)

a4=uint64(200)

b1=single(3.5)

b2=12

c1=true

c2{1,1}=100

c3='hello'

c4.name='robin'

d=@cos

运行得结果

a1 =

    6

a2 =

    -20

a3 =

         100

a4 =

                  200

b1 =

    3.5000

b2 =

    12

c1 =

     1

c2 =

    [100]

c3 =

hello

c4 =

    name: 'robin'

d =

@cos

查看变量的数据类型在命令行窗口输入whos

得到

Name        Bytes    Class    

  a1             1          int8     

  a2             2          int16

  a3             4          uint32

  a4             8          uint64

  b1             4          single

  b2             8        double

  c1             1         logical

  c2            68          cell

  c3            10          char

  c4           134         struct

  d              16         function_handle

基本数据类型

数据类型                                          说明

int8,,int16,int32,int64                       有符号整型

uint8, uint16, uint32, uint64              无符号整型

single                                              单精度浮点数

double                                             双精度浮点数

logical                                                逻辑型

cell                                                    单元数组型

char                                                  字符串型

struct                                               结构体型

function_handle                               函数句柄型

1.1.整数

关于int和uint后面的数字是占用的字节

8对应1字节,16对应2字节…

输入

a=24;

b1=int8(a)

b2=int16(a)

b3=int32(a)

b4=int64(a)

c='hello'

int8(c)

结果

b1 =

   24

b2 =

     24

b3 =

          24

b4 =

                   24

c =

hello

ans =

  104  101  108  108  111

关于取整的函数

round:向最接近的整数取整,如果小数为0.5,则取绝对值最大的整数

fix:向0取整

floor:不大于该数的最接近整数

ceil: 不小于该数的最接近整数、

输入

a1=round(2.5)

a2=round(-2.4)

a3=round(-2.5)

b1=fix(-3.6)

b2=fix(-3.5)

c1=floor(4.9)

c2=floor(-4.2)

d1=ceil(4.2)

d2=ceil(-4.4)

结果

a1 =

     3

a2 =

    -2

a3 =

    -3

b1 =

    -3

b2 =

    -3

c1 =

     4

c2 =

    -5

d1 =

     5

d2 =

-4

1.2.浮点数

包括单精度和双精度

单精度浮点数比双精度浮点数能够表示的数值范围和数值精度都小

输入

a=123.34

b=single(a)

c1=double(a)

c2=int16(a)

c3=int32(a)

d1=[realmin('single') realmax('single')]

d2=[realmax('double') realmax('double')]

结果

a =

  123.3400

b =

  123.3400

c1 =

  123.3400

c2 =

    123

c3 =

         123

d1 =

   1.0e+38 *

    0.0000    3.4028

d2 =

  1.0e+308 *

1.7977    1.7977

1.3.复数

是对实数的扩展

complex(a,b):创建复数,a为实部,b为虚部

real(z):得到复数z的实部

imag(z):得到复数z的虚部

abs(z) :得到复数z的模

angle(z) :得到复数z的角度

conj(z) :得到复数z的共轭复数

输入

z1=3+4i

a1=real(z1)

a2=imag(z1)

b1=abs(z1)

b2=angle(z1)

c1=conj(z1)

z2=complex(1:3,2:4)

real(z2)

imag(z2)

结果

z1 =

   3.0000 + 4.0000i

a1 =

     3

a2 =

     4

b1 =

     5

b2 =

    0.9273

c1 =

   3.0000 - 4.0000i

z2 =

  Columns 1 through 2

   1.0000 + 2.0000i   2.0000 + 3.0000i

  Column 3

   3.0000 + 4.0000i

ans =

     1     2     3

ans =

     2     3     4

1.4.数据的显示格式

可以采用函数format()确定,改变数值类型的显示格式后,一直有效,直到再次使用format()进行修改

输入

format short

a=12.3456789

format short

a

format long

a

format long e

a

format short e

a

format bank

a

format +

a

format rational

a

结果

a =

   12.3457

a =

   12.3457

a =

  12.345678899999999

a =

     1.234567890000000e+01

a =

   1.2346e+01

a =

         12.35

a =

+

a =

1000/81   

默认显示格式是format short

2.逻辑类型

MATLAB中用1代表逻辑真,用函数true表示;用0代表逻辑假,用函数false表示。也可以用logical()将数值类型转换为逻辑型(任何非0数值转为逻辑真,0数值转为逻辑假)。

输入

a1=true

a2=false

a3=true(3,4)

a4=false(3)

结果

a1 =

     1

a2 =

     0

a3 =

     1     1     1     1

     1     1     1     1

     1     1     1     1

a4 =

     0     0     0

     0     0     0

     0     0     0

3.字符和字符串

MATLAB中字符和字符串不进行区分,都用“ ’ ”括起来

输入

a='My name is 张三'

b=char([65 66 67 68])

c=int8('hello')

d='张'

结果

a =

My name is 张三

b =

ABCD

c =

  104  101  108  108  111

d =

4.函数句柄

通过函数句柄来间接调用函数,可以通过“@”后面跟函数命令来创建。

func2str(fhandle):将函数句柄转换为字符串

str2func(str):将字符串转换为函数句柄

functions(fhandle):返回包含函数信息的结构体变量

isa(a,’function_handle’):判断是否为函数句柄

isequal(fhandle1, fhandle2):检测两个函数句柄是否对应同一函数

输入

f1=@cos

t=0:pi/5:pi

f1(t)

f2=@complex

f2(3,4)

结果

f1 =

    @cos

t =

  Columns 1 through 4

         0    0.6283    1.2566    1.8850

  Columns 5 through 6

    2.5133    3.1416

ans =

  Columns 1 through 4

    1.0000    0.8090    0.3090   -0.3090

  Columns 5 through 6

   -0.8090   -1.0000

f2 =

    @complex

ans =

   3.0000 + 4.0000i

输入

f1=@char

s1=func2str(f1)

f2=str2func(s1)

functions(f1)

isa(f1,'function_handle')

isequal(f1,f2)

结果

f1 =

    @char

s1 =

char

f2 =

    @char

ans =

    function: 'char'

        type: 'simple'

        file: 'MATLAB built-in function'

ans =

     1

ans =

     1

5.单元数组类型

是一种比较特殊的数据类型,每个元素都以单元的形式存在。常采用“{}”建立单元数组,也可以用函数cell()来建立单元数组(在获取单元数组中的元素时,也用{}表示下标)。

5.1.输入

c={'中国','China';[1 2 3 4 5],100}

c{1,1}

c{2,1}

c{2,2}=[]

结果

c =

    '中国'            'China'

    [1x5 double]    [  100]

ans =

中国

ans =

     1     2     3     4     5

c =

    '中国'            'China'

[1x5 double]         []

5.2.cell()

输入

c=cell(2,3)

c{1,1}=[1:3;3:5]

c{2,2}='China'

c{2,3}='Robin'

c{2,1}=100

结果

c =

    []    []    []

    []    []    []

c =

    [2x3 double]    []    []

              []    []    []

c =

    [2x3 double]         []    []

              []    'China'    []

c =

    [2x3 double]         []         []

              []    'China'    'Robin'

c =

    [2x3 double]         []         []

[       100]    'China'    'Robin'

5.3.celldisp()

输入

c={eye(2),'China';[1:4],100}

celldisp(c)

celldisp(c,'mycell')  %结果命名为mycell

结果

c =

    [2x2 double]    'China'

    [1x4 double]    [  100]

c{1,1} =

     1     0

     0     1

c{2,1} =

     1     2     3     4

c{1,2} =

China

c{2,2} =

   100

mycell{1,1} =

     1     0

     0     1

mycell{2,1} =

     1     2     3     4

mycell{1,2} =

China

mycell{2,2} =

   100

5.4.cellplot()

输入

c={'中国','China';[1:4],100}

figure;

out=cellplot(c,'legend')

结果

c =

    '中国'            'China'

    [1x4 double]    [  100]

out =

  174.0023

  175.0018

  176.0018

  177.0018

  178.0018

  179.0018

  180.0018

  181.0018

5.5.iscell()和num2cell()

输入

a=[2.3 4.5 9.2;3.5,3.2 8.5]  

c=num2cell(a)                          %将矩阵转换为单元数组

iscell(a)

iscell(c)

结果

a =

    2.3000    4.5000    9.2000

    3.5000    3.2000    8.5000

c =

    [2.3000]    [4.5000]    [9.2000]

    [3.5000]    [3.2000]    [8.5000]

ans =

     0

ans =

     1

5.6.cell2struct()

输入

c={'Dr. Zhang',1.80,20110001,[89 90 96]}

fields={'name','height','num','score'} %结构体变量

s=cell2struct(c,fields,2)

结果

c =

  Columns 1 through 3

    'Dr. Zhang'    [1.8000]    [20110001]

  Column 4

    [1x3 double]

fields =

    'name'    'height'    'num'    'score'

s =

      name: 'Dr. Zhang'

     height: 1.8000

       num: 20110001

      score: [89 90 96]

5.7.删除元素

输入

c={'中国','China';[1:4;2:5],10}

c{2,4}=100                                     %单元数组的扩充

c(1,:)                                         %第一行元素

c(:,3)=[]                                       %删除第三列

d={'北京','Beijing';[],3}

e=[c,d]                                %单元数组的合并

e(2,:)=[]                               %删除第二行

结果

c =

    '中国'            'China'

    [2x4 double]    [   10]

c =

    '中国'            'China'    []       []

    [2x4 double]    [   10]    []    [100]

ans =

    '中国'    'China'    []    []

c =

    '中国'            'China'       []

    [2x4 double]    [   10]    [100]

d =

    '北京'    'Beijing'

      []    [      3]

e =

  Columns 1 through 4

    '中国'            'China'       []    '北京'

    [2x4 double]    [   10]    [100]      []

  Column 5

    'Beijing'

    [      3]

e =

'中国'    'China'    []    '北京'    'Beijing'

6.结构体类型

和C类似。

6.1.struct:建立结构体变量

输入

s1=struct('type',{'big','little'},'color',{'red'},'data',{[1 2 3;2 3 4] [1:4]})

s1(1)

s1(2)

结果

s1 =

1x2 struct array with fields:

    type

    color

    data

ans =

     type: 'big'

    color: 'red'

     data: [2x3 double]

ans =

     type: 'little'

    color: 'red'

     data: [1 2 3 4]

6.2.rmfields:删除结构体的成员变量

s1=struct('type',{'big','little'},'color',{'red'},'data',{[1 2 3;2 3 4] [1:4]})

s2=rmfield(s1,'color')

s3=rmfield(s1,{'type','color'})

6.3.isstruct:判断是否为结构体

isfield:判断是否为结构体成员

s=struct('one',1,'two',2)

f1=isstruct(s)

f2=isfield(s,'one')

f3=isfield(s,'three')

f4=isfield(s,{'one','pi','Two','three'})

6.4.fieldnames:获得结构体变量成员的名字

orderfields:按照成员变量字母顺序对结构体成员排序

s=struct('one',1,'two',2,'three',3,'four',4)

f1=fieldnames(s)

f2=orderfields(s)

6.5.getfields:得到结构体的成员变量的值

s1=struct('one',1,'two',2)

f1=getfield(s1,'one')

s2=struct('name',{'Robin','Tom'},'sex',{'Male','Male'},'score',[87 89 94])

f2=getfield(s2,{1,2},'name')

f3=s2(1,2).name

f4=getfield(s2,{1,2},'score',{2})

f5=s2(1,2).score(2)

6.6.setfields:设置结构体的成员变量的值

s1=struct('one',1,'two',2)

s2=setfield(s1,'one',3)

s3=struct('name',{'Robin','Tom'},'sex',{'Male','Male'},'score',[87 89 94])

s4=setfield(s2,{1,2},'score',[78 76 99 81])

s3(1,2).score

s4(1,2).score

6.7.struct2cell:结构体变量转换为单元型变量

cell2struct:单元型变量转换为结构体变量

s(1).name='Robin';

s(1).sex='Male';

s(1).num=20110001;

s(1).room=203;

s(2).name='Tom';

s

c=struct2cell(s)

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值