请选择 进入手机版 | 继续访问电脑版

QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2381|回复: 1

MATLAB数据类型

[复制链接]
字体大小: 正常 放大

326

主题

32

听众

1万

积分

  • TA的每日心情
    慵懒
    2020-7-12 09:52
  • 签到天数: 116 天

    [LV.6]常住居民II

    管理员

    群组2018教师培训(呼和浩

    群组2017-05-04 量化投资实

    群组2017“草原杯”夏令营

    群组2018美赛冲刺培训

    群组2017 田老师国赛冲刺课

    发表于 2020-1-20 16:59 |显示全部楼层
    |招呼Ta 关注Ta
    MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。# ~1 ?( t6 I: M" G$ y! m  w( I
    5 Q5 K# s. E6 }0 G' D
    如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。/ v9 p7 E7 x8 Y5 b  q
    ( `! ?  p% I$ P& Q% t
    例如,
    5 d4 R, [! {& X  {. z7 R) I" D  j; h, Y+ U  j) t" c8 R4 R6 ]
    Total = 42; z+ Q7 Q3 Q3 ^7 v2 a
    上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。
    . L- O3 D3 b$ q( g' }& R9 L: }" `1 L. [8 P5 a
    MATLAB中可用的数据类型! x9 }( h/ L0 m0 e; U, `
    2 o; `6 V! F/ C3 t( S! s
    MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
    ( r$ A2 s2 ]1 ?7 c+ N. S
      O) y: [. R0 O! K9 T+ P下表显示了在 MATLAB 中最常用的数据类型:
    / @$ k! `8 q: n$ b- b7 y4 H
    0 u) m+ c5 j* `* E/ s数据类型        描述
      G4 e4 }7 M. H, N  j9 l! n, M2 \int8        8-bit signed integer
    5 c; T2 q/ G& q& p2 E1 kuint8        8-bit unsigned integer2 e5 m. E- G2 c
    int16        16-bit signed integer
    7 {  r2 J& N7 H8 z4 w  V9 u" zuint16        16-bit unsigned integer
    & a) _2 A, P) p4 @* P/ fint32        32-bit signed integer( {0 Q* r0 i* {
    uint32        32-bit unsigned integer9 l/ K; N! p4 `+ ]/ h
    int64        64-bit signed integer8 D/ k. k: {1 U( Y; S& c- g  u
    uint64        64-bit unsigned integer+ f' ^' {1 L0 G0 B  J  z4 V# u. m% z% P
    single        single precision numerical data
    9 T/ L  J2 N3 h) @9 ?1 e* cdouble        double precision numerical data
    5 H0 I6 d" d, u% zlogical        logical values of 1 or 0, represent true and false respectively
    ) R; U4 O! H; j8 t5 E$ Pchar        character data (strings are stored as vector of characters)
    * m5 l0 _3 G- |$ o% tcell array        array of indexed cells, each capable of storing an array of a different dimension and data type# h9 L# H/ Y& h& V: W; E
    structure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
    * [4 I7 O6 ~9 rfunction handle        yiibaier to a function) I# Q; H9 [! g7 ]
    user classes        objects constructed from a user-defined class
    + W6 ^- l0 W/ F9 \java classes        objects constructed from a Java class
    2 S9 w4 j  r$ d( ?6 M. @例子
    6 D2 m- u( `1 y) }5 Y6 c0 b
    + s2 N5 W1 ~$ B9 H3 y( S. J" N创建一个脚本文件,用下面的代码:3 d2 d4 j5 T' b7 @$ u9 `& k0 Y  B

    3 w; I/ L% j" f; | str = 'Hello World!'6 C; R" s! B$ y& k
    n = 2345, h8 O9 O+ E6 I( J& z1 }
    d = double(n)% }; |- p& Z$ C/ k& h3 q
    un = uint32(789.50)+ U& Q5 e: F1 q  N* O! i3 B; n/ a1 T
    rn = 5678.92347
    ( v( s2 g* r2 u( b  jc = int32(rn)
    ! d3 ]2 O% j7 Y5 f" _, k8 O2 D* H4 m上面的代码编译和执行时,它会产生以下结果:
    + s6 i& W7 \( p! \# Q' n/ p- H& i" H( v  w- w
    str =
    + V) d7 L, _: UHello World!
    : h- w' S% Q3 U0 B8 s) F/ on =
    7 y* F  d* g$ m* R* O   23457 w  Z1 d; {8 {7 E' t
    d =
    : O9 ^5 R( L, @   2345) l) s+ A1 H- ]: f8 a6 d: k7 _3 [# X
    un =
    - D* L( @' @" P/ u. m1 A) l% ~   790/ G* r% b/ z5 J+ p0 J& E
    rn =9 y1 ~* H4 V  `1 l9 m# D# U
       5.6789e+03
    9 F  ], q) _0 H) Y0 O2 \/ z$ Yc =8 W0 T! s' @3 B8 r- v7 T: E3 n4 f
       5679: ^$ J8 Z: V2 }6 b. ~  `
    数据类型转换
    4 O" V2 v5 B+ m& P* P5 d" Q8 j& x% j5 w( M7 p% }
    MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:- V$ H0 r# Q3 U5 w

    . \# d2 W- K+ a! c# n) U函数        目的/作用
    ' y7 @; j: L# ^char        Convert to character array (string)
    ! N0 t/ O$ T; i& P" Mint2str        Convert integer data to string
    3 d1 F' V0 c. Y$ x) Amat2str        Convert matrix to string
    2 p2 D8 e' E4 Ynum2str        Convert number to string
    7 x4 p* _1 t0 g( |: [6 ]9 hstr2double        Convert string to double-precision value
    + Y) c) W) U  L; D6 S* Z# C) X8 q9 jstr2num        Convert string to number
    " M9 B5 f' K2 x+ T. |& \' o$ znative2unicode        Convert numeric bytes to Unicode characters
    8 A& J$ f# e# x; eunicode2native        Convert Unicode characters to numeric bytes# y9 J7 W& w0 ~! V. C
    base2dec        Convert base N number string to decimal number$ s4 q6 @, H( ^
    bin2dec        Convert binary number string to decimal number, R) f8 N5 @1 Q4 d! [7 s. c
    dec2base        Convert decimal to base N number in string
    ; z" {8 {, O$ fdec2bin        Convert decimal to binary number in string
    3 n, D6 Q2 _+ h8 F5 F7 _dec2hex        Convert decimal to hexadecimal number in string
    9 d) z9 z# b: ^- ?  F3 ahex2dec        Convert hexadecimal number string to decimal number& t% l9 [2 v  [; u8 h
    hex2num        Convert hexadecimal number string to double-precision number9 a! Z1 X% }# @# z% b' n
    num2hex        Convert singles and doubles to IEEE hexadecimal strings
    ( L5 x# b3 u# k$ @4 t1 [) x, Mcell2mat        Convert cell array to numeric array
    ) D% J# C$ j. ]6 C3 K  U' E; m( ycell2struct        Convert cell array to structure array
    3 Q) z1 x% r& K% G5 ecellstr        Create cell array of strings from character array
    6 K+ U6 ]! ^) E8 L! emat2cell        Convert array to cell array with potentially different sized cells5 ~# R3 }6 \# T
    num2cell        Convert array to cell array with consistently sized cells
    , u( z# v3 T" {2 y& p8 V, Zstruct2cell        Convert structure to cell array
    : e" B1 L6 t, Q" Y  a测定的数据类型
    ) k: j* d7 J) m; x" Q% p1 H. U4 k. r7 h  ?! |2 n
    MATLAB 提供各种函数标识数据类型的变量。4 ^- x; c. ?; u, @
    + V1 E# F  J# H  j9 K* l
    下表提供了确定一个变量的数据类型的函数:
    ) r# E4 l  D  _1 x5 w  l. b* j# `5 b
    6 k  \7 x& ^$ ~2 A9 g函数        目的/作用' X7 d+ y' P- W% F" H
    is        Detect state
    2 X+ R6 ?& I# _5 F- G  C% Cisa        Determine if input is object of specified class, K: n  \8 c3 l8 d
    iscell        Determine whether input is cell array- Q+ j6 W! C: i0 Y
    iscellstr        Determine whether input is cell array of strings
    2 K+ j3 b5 T  c' r8 k$ F0 I' ?ischar        Determine whether item is character array
    ) o3 V: B8 l- L& y. ^& n! Z* L4 Uisfield        Determine whether input is structure array field
    4 o% V* U  h7 sisfloat        Determine if input is floating-yiibai array
    0 [8 H0 M) V: K# f% yishghandle        True for Handle Graphics object handles
    " @# a* t8 @* ~isinteger        Determine if input is integer array1 ^% m5 g: |. ~7 @/ z+ N
    isjava        Determine if input is Java object' U- M: K5 i- U$ \" w2 h6 l' O- Z
    islogical        Determine if input is logical array
    8 p) H, F6 a1 d9 O" f( g7 f0 Nisnumeric        Determine if input is numeric array
    7 P8 I1 t) O/ eisobject        Determine if input is MATLAB object
    5 S8 w. @9 U: ?5 d4 ~! Lisreal        Check if input is real array
    # J, V4 Z$ J2 m* q" Disscalar        Determine whether input is scalar) x3 \4 W0 w0 B& s
    isstr        Determine whether input is character array& H/ k2 w1 Z" J& D' x5 z
    isstruct        Determine whether input is structure array
    ; U+ K3 X7 U9 gisvector        Determine whether input is vector7 a& C  H) q' E* @
    class        Determine class of object
    ( W% Z: X2 t' x$ y! `/ svalidateattributes        Check validity of array
    & z2 ]. {. ~4 U0 s1 Z; nwhos        List variables in workspace, with sizes and types8 j$ \$ T! H" \; t5 v
    例子7 U! ~" k# j' t5 J& [  C, r8 U

    % `- ?' ~* }9 h4 o创建一个脚本文件,用下面的代码:, _/ Z! _8 ]* u) p: D1 A1 |' F6 k

    - R. ]( N% o. m4 d1 o' D# |' e8 s3 rx = 3
    2 c1 {1 }3 I7 ^9 E; Uisinteger(x)) d- g" l2 R! G, p4 l; ?
    isfloat(x)& B& W0 M! V' _* U2 q3 ~
    isvector(x)
    ; x5 Z! v- J( y0 G5 F) z% z% {isscalar(x)
    6 S+ {) y& a' H0 N  Yisnumeric(x)- ^  s7 c  u# q

    2 F2 U! t: H9 [# G+ ]7 mx = 23.546 _& i; b. Z0 d* B
    isinteger(x)
    6 g' Y" G" X7 O: p2 k0 \5 ~isfloat(x)$ s: n$ O! `; D9 [
    isvector(x)4 i" I9 W7 p$ x9 u% u9 V  C" q
    isscalar(x)2 \7 A: V/ m4 N( h" w8 N
    isnumeric(x)+ |0 y  D# v0 b. c
    / j/ P5 i5 S, a; H
    x = [1 2 3]
    8 u5 L+ t2 @- Iisinteger(x)
    0 U+ {) {- Z# f: j( X; Q3 disfloat(x)( }* X) ]$ F3 H3 U% W9 t! n
    isvector(x)( N; _8 r0 [0 d' M5 s2 B. Z
    isscalar(x)) }& K8 c: e1 A, k5 I2 y, ^: k

    1 I0 l9 ]4 c6 O/ Ix = 'Hello'$ v# _% z8 g% [! k, S/ P
    isinteger(x)
    3 Z  H! W5 G8 w. T+ O7 r0 Kisfloat(x)9 W+ E" {+ P+ M0 k0 q; D
    isvector(x)
    & Q( S: V) V) {! D; _5 {" x# ?isscalar(x)( L8 q- O9 T' C8 O6 D( o
    isnumeric(x)
    8 t( ^( e: {" d& o当运行该文件,它会产生以下结果:
    0 P. z; _& f' G# ?! H7 `6 c4 d0 W  R3 c  n2 U- M% L+ N% e
    x =0 V5 A2 s" s: h) M% m
         30 {* S  L. i8 F5 t! b0 p% c
    ans =( T; Y' E/ g1 D) e/ a
         0& b: b* X; X) c, h, `
    ans =1 ~5 T0 A: H( }1 v
         1
    . H7 J4 k+ H% n( ?ans =
    - f4 z4 p1 V: l* y' l     1' A( b, ~: A8 q+ X
    ans =
    6 X9 p: j# p6 m' B: ^* a& M     1& X+ f9 R6 I/ c# m8 K) C, S
    ans =# f& n+ }. T0 Z- P+ @& D
         1. S7 k+ f3 d$ ]4 k' e  `
    x =" q! G. k4 _* G; m6 _! ?
       23.5400
    ) ]% v, ?9 d' o, L3 \3 ?9 J$ mans =1 k9 |; d9 f: q+ B* V
         0
    ( h' ]. x1 |/ U& w/ L7 j) f* Qans =
    5 W2 J4 I) f0 t$ y4 V: U* ?     1
    ; }- e5 J7 R  y5 Y' _- }9 bans =
    & ?7 `, m# t  a5 A0 y% ?. ]2 Q( q& Z     1
    & n5 f& ?/ b7 S! N& ?: R) Cans =5 ~3 H( z# s# _4 _
         1  o' H1 q* r+ l* B' j. h8 P% D2 Y& L
    ans =
    ! \6 s! D! ?; \; u* I+ L     1! c1 F) W4 I$ H& e7 u8 o  L/ E
    x =0 l% i+ B/ R5 L! i& \
         1     2     31 g9 W' Q, @( }$ U0 ~
    ans =
    " F: j0 n# d4 ]     0
    ; ~) R, }+ E$ M8 \+ A4 m% j4 ?ans =7 [; F/ N% Y. G  f: x' R
         1
    & E9 V$ x1 _9 \# T& o. c$ ^ans =/ v% e1 l' i  }: v5 m
         1
    , k. R! E% v; D2 x* ^ans =
    2 _) ]- y/ D7 W1 p) R     0
    8 A5 W! D0 m' J$ d' T5 ux =
    $ ~/ W" _9 s' f& A+ LHello& r  R# N: E: ^) Y
    ans =
    6 N% r5 E% [9 W2 N9 x$ b, c     0
    ( ~  c5 d4 M8 \3 Yans =
    ; K8 }1 t! l9 j8 j  F$ X     0
    4 a# J  v, h: D5 M4 ]ans =3 V& p- X8 K& O! a" c
         1
      ]% M# P. f- @  x5 [2 Wans =
    , H4 h! L6 v5 V/ c  h     0! w+ p7 j" z3 x" Y* Z7 i
    ans =
    . i0 N7 L2 Z2 J     0
    ; M3 f( U; E- T" x1 r3 m. [4 j' g9 y7 W
    ————————————————
    8 |$ Z$ s' [5 P5 v& k  q版权声明:本文为CSDN博主「Phil__ming」的原创文章。. {" T3 T, X) C( {  G4 n6 I
    原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575; L( b2 y  Q8 d2 M

    3 t; P1 L; L# F# J2 v& [+ a; U  Z
    zan

    0

    主题

    1

    听众

    59

    积分

    升级  56.84%

  • TA的每日心情
    开心
    2020-3-22 18:15
  • 签到天数: 14 天

    [LV.3]偶尔看看II

    邮箱绑定达人

    群组数学建模美赛备战群组

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2024-4-18 14:20 , Processed in 0.281106 second(s), 57 queries .

    回顶部