QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3391|回复: 1
打印 上一主题 下一主题

MATLAB数据类型

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

326

主题

32

听众

1万

积分

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

    [LV.6]常住居民II

    管理员

    群组2018教师培训(呼和浩

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

    群组2017“草原杯”夏令营

    群组2018美赛冲刺培训

    群组2017 田老师国赛冲刺课

    跳转到指定楼层
    1#
    发表于 2020-1-20 16:59 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
      ]6 z# r' v2 {0 h) _
    ; V2 [# q& l- s如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。
    % Q9 T  a/ x6 B  c# h  l, e/ N* H, e- ?( b
    例如,% D' |7 Y( |+ Q2 `( L

    7 V* u4 l" c4 V# U; K1 nTotal = 42# G2 j3 f" a; v" ]2 y
    上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。% @; v9 O. p0 u+ q0 i6 J% Y5 i

    / x1 x& u/ H( `; ]9 \9 c& RMATLAB中可用的数据类型) S2 b( F7 t9 j5 f* P

    % N; Z- E( X( C3 @MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
    $ R* T) `: ~- Z- Y# {( v
    4 [/ _# \3 x) D3 a) W下表显示了在 MATLAB 中最常用的数据类型:
    * n5 A" T2 a3 D9 g( d( }. Y
    3 B, P- j. _6 s' o; M& g' {数据类型        描述
    7 n/ v& l; l5 u& }int8        8-bit signed integer
    3 |' ^& u/ o2 Q  w* ^: S( Wuint8        8-bit unsigned integer
    + f4 B7 a1 }3 g. }+ Tint16        16-bit signed integer; E, Z. `- J% l4 s' S- E; s
    uint16        16-bit unsigned integer1 x" D3 `7 e. O: T9 Z4 ^
    int32        32-bit signed integer
    . ]2 j' g  v% O; F+ ~7 Juint32        32-bit unsigned integer
    $ q3 p1 e3 }# y5 j5 i) qint64        64-bit signed integer
    / G5 Y# n6 y: O8 l7 Fuint64        64-bit unsigned integer
    # l" x0 h% Z& c1 R2 L' ?; i% Rsingle        single precision numerical data* e: n2 d! \: ^, U
    double        double precision numerical data: |2 I" |6 w& a
    logical        logical values of 1 or 0, represent true and false respectively
    . |' i/ D" R$ Z4 w- Qchar        character data (strings are stored as vector of characters)
    + I0 o& |7 j5 `+ Lcell array        array of indexed cells, each capable of storing an array of a different dimension and data type  J2 A7 L" `, o8 R+ d. |- a7 \
    structure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type) K* Q5 ~( V; \- e8 G2 Y! [
    function handle        yiibaier to a function
    8 c# Q- `' N( f7 j8 e+ H( h0 t6 B' Fuser classes        objects constructed from a user-defined class7 x% i/ ^  [3 p7 {4 u. {
    java classes        objects constructed from a Java class* D" }1 C1 [! n6 a- K  X3 L
    例子7 W* T7 S+ h- _$ o
    ( s" X$ j8 O4 B4 p! g% r
    创建一个脚本文件,用下面的代码:: f0 j: t9 ]. u. A1 K
    - O/ U  Q3 v( G- B$ N1 l& H" t
    str = 'Hello World!'
    % N) E) f- Y  e$ p  e8 w* ]5 [) qn = 23452 C0 ~: S9 J+ L: b
    d = double(n)) G7 k9 h- M' N  r0 j2 Y
    un = uint32(789.50)9 P3 r  _# R) ~2 ?
    rn = 5678.92347# t) C7 z1 O7 O2 N+ r
    c = int32(rn)9 w9 m( G9 t' k. r
    上面的代码编译和执行时,它会产生以下结果:) @$ [8 F& H' k7 `  w  ?

      U& I2 h/ p7 x7 [str =
    , C  N! D1 W# r* z. WHello World!
    / b3 ~, ]. N/ ~n =
    + ]8 f$ d( s6 s3 t$ j' [   2345
    . Q1 _# G/ Z3 e& Z" ^- id =! S; P7 Y; Z" U# B* x4 t# S
       2345/ V$ z6 O- c" P! g" p: i6 M
    un =; t. H1 D0 n$ t' h* n
       790
    ' H( z8 d; U8 R! r: H9 lrn =# [/ D2 ?6 [3 k. S+ {8 v
       5.6789e+03
    $ M- I  \3 k. t. gc =
    + W+ r& R  Q( x1 n   5679
    ) }4 u6 l$ @0 l) o% d# j数据类型转换4 q5 S8 ]+ w7 j. e% v6 r+ M" t; [) b

    ! W; F) }( U( k' Z9 i6 {3 n6 p1 `9 tMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:/ Y4 y9 B$ k! w  a( c, s

    $ Q$ b# b" ?  R2 z: [函数        目的/作用3 \6 P* _, r/ D! T6 L
    char        Convert to character array (string)# f" W/ @0 k! H5 S# f
    int2str        Convert integer data to string6 _2 q5 Z! S: n/ A% i1 ^
    mat2str        Convert matrix to string
      R( p. U4 y8 |num2str        Convert number to string, f! x1 q% s3 v3 a
    str2double        Convert string to double-precision value
      Z2 q. T6 z. `str2num        Convert string to number
    9 n" z) Q7 m  R. G! t( i$ mnative2unicode        Convert numeric bytes to Unicode characters5 w; |) v7 s* L& ~7 Y" e
    unicode2native        Convert Unicode characters to numeric bytes( i/ a& w* n' l. h) @9 b* H, H
    base2dec        Convert base N number string to decimal number1 A% U# F7 b% K3 i& g- X
    bin2dec        Convert binary number string to decimal number) D. n* `/ F) C# G- e+ L
    dec2base        Convert decimal to base N number in string
    $ A5 D$ E& l7 `dec2bin        Convert decimal to binary number in string. t/ ?2 l' ~  F6 _, L2 B
    dec2hex        Convert decimal to hexadecimal number in string
    5 U* j6 {. u5 f+ P% mhex2dec        Convert hexadecimal number string to decimal number
    1 c7 E7 i/ M3 C2 g+ ?0 K& \& ?hex2num        Convert hexadecimal number string to double-precision number
    4 X% M1 k# r( Z1 d! snum2hex        Convert singles and doubles to IEEE hexadecimal strings
    * g% `" S/ f3 m# g, ucell2mat        Convert cell array to numeric array
    / b+ l2 M% l" \! tcell2struct        Convert cell array to structure array
    ' E3 _& n/ t& r  Y. w. q& g" }- b( J% zcellstr        Create cell array of strings from character array
    # x# J- `" r% m7 t& B% x. rmat2cell        Convert array to cell array with potentially different sized cells
    9 ?: E" D; l1 M0 inum2cell        Convert array to cell array with consistently sized cells$ x- o6 P0 x, w2 ^9 `
    struct2cell        Convert structure to cell array. o% e/ w3 q+ ?
    测定的数据类型' h5 d: e7 Y( P3 M# B* P

    1 i- a2 w4 f. A+ T5 ^* U8 B6 t3 wMATLAB 提供各种函数标识数据类型的变量。
    ( d1 u+ F5 L3 J- Z) i+ t. Y6 k4 T# K+ N' h1 f$ Z, }
    下表提供了确定一个变量的数据类型的函数:
    # q: H+ `& s8 {3 a6 E( [' w1 R/ d1 N
    函数        目的/作用- @& q' \) Y6 z9 I9 H! X: i
    is        Detect state
    9 e6 B  K9 @  b" a2 L% e4 A7 @& Disa        Determine if input is object of specified class
    5 ?+ h- L" u) @) hiscell        Determine whether input is cell array
    3 |8 q$ o* v, W. q2 Y" Wiscellstr        Determine whether input is cell array of strings
    ' ?" G  \' ~$ S" G% _ischar        Determine whether item is character array
    + }9 z" L* S4 w9 h3 E8 J% oisfield        Determine whether input is structure array field
    4 `4 e/ U3 H; |/ A+ \isfloat        Determine if input is floating-yiibai array" y/ o; D9 v. `) \1 ~/ j! X! }3 G
    ishghandle        True for Handle Graphics object handles6 W% `6 E6 ^2 b" g
    isinteger        Determine if input is integer array1 \4 f: T) G, F% s( r
    isjava        Determine if input is Java object7 g9 m+ h+ g  G6 W: T4 @8 H  ^
    islogical        Determine if input is logical array
    , B' c) t6 ^5 r- u1 [& qisnumeric        Determine if input is numeric array4 E; }, [; x! a. M$ X: Z
    isobject        Determine if input is MATLAB object& Q6 K# V9 s$ J" |9 Q) B
    isreal        Check if input is real array
    1 P) U' l0 G( \, M3 Fisscalar        Determine whether input is scalar
    1 `" z# S8 C3 O( S' n/ pisstr        Determine whether input is character array
    - h1 i' @4 L3 W/ Risstruct        Determine whether input is structure array
    + n# p' a% n& i: hisvector        Determine whether input is vector
    8 D9 |* f  \5 u; V2 G7 jclass        Determine class of object
    ) v4 d- v0 l2 W3 k0 `6 O" \validateattributes        Check validity of array- O$ ]7 V+ {$ @2 Y
    whos        List variables in workspace, with sizes and types
    8 M0 [" e7 r+ v5 r( o- ~例子
    % _1 \+ q# Y) R& ]
    5 |, Y# d9 V4 e5 h7 L) M4 ]创建一个脚本文件,用下面的代码:
    ( c+ p) H- R3 k3 m! H0 F, }
    9 a1 y* t: ]+ Zx = 3
    9 e& N0 |& Q# m7 m* D4 i5 qisinteger(x)
    9 P& N6 f* a3 G2 a5 `4 N8 Q1 Jisfloat(x), @( X5 Y) J$ m) b  g1 ~
    isvector(x)
      m, G' t0 i" N9 U, _% v2 |isscalar(x)" s' W& {# o7 }4 R- u
    isnumeric(x)3 ]7 i% H" F0 e- X

    $ Y1 e! ^7 C; Y3 T/ z- A% Ax = 23.54$ j* |  h; G5 t$ j0 X7 x# \1 ~
    isinteger(x)
    $ ^- ?% d0 l! Sisfloat(x)
    7 ~! ~% N6 v5 W. `8 W+ c% t" aisvector(x)
    $ K8 r. y2 }1 V* }isscalar(x)6 E2 U- `" f) }1 w) ~) W+ j: @
    isnumeric(x)7 Z1 W& l8 b9 h7 x
    . m, f4 p4 S4 i! b! {5 E
    x = [1 2 3]; [* @7 q7 r& J! w7 X
    isinteger(x)
    , b7 D9 M2 ?" }3 {' v! hisfloat(x)
    . d1 J" I. y* Q, f" C4 f2 eisvector(x)
    2 h3 P" B9 @- f) E& \9 E* gisscalar(x)
    / D; H) @  z/ Q( l& g+ u
    / E8 o5 X0 H% i5 {9 Y4 Y5 ]) dx = 'Hello'
    2 }$ ]' D% X1 O/ d& a; I& v) {isinteger(x)2 G; X, `+ d3 @/ A
    isfloat(x)$ K1 I2 t+ u* L
    isvector(x)
    $ X3 b' L$ d% W: U8 O8 K! Q( Eisscalar(x)3 n* k+ Y( Z. Z" b
    isnumeric(x)
    + A/ G+ r* U7 ?+ g( i2 n# o8 s当运行该文件,它会产生以下结果:
    + X8 |3 r: ~. a$ s0 o
    ; v( t3 n  s* b2 s( nx =% y; U# a1 |: t# F- H" s; O# M
         3( x* p: ?' B' U) Z+ j1 u- i8 h
    ans =. T% {6 t) g" W- A; m# g) t4 a
         0
    ( X0 M. r- z+ b( D1 {4 Hans =
    # \. f4 J/ V: B' m5 W  _9 w     14 `- l+ t; E; Q6 j- m' F6 O
    ans =; o8 A8 u1 b( v
         1! @4 W9 w; u' {% n" b/ E6 X) ^$ ]
    ans =
    8 z7 q$ i0 S/ q2 X+ ^3 C8 ~+ b     18 v6 y; }+ l, c% Z' G# H: Y
    ans =9 g  B! l* _$ `& N
         1
    ) @) S7 V$ L7 s! v" Z5 e" N6 Dx =8 ^# f* G8 D3 I4 T! {! J6 f! U
       23.5400
    & s# j) Z: b% ^3 ~ans =
    + C( s' u; g! G9 O% M1 c$ ?- h( _     0+ Q: z5 K- w3 J: U
    ans =* y, u* S% u' z2 U
         1
    8 _( q  V+ F+ \5 F) ^* W+ ^6 J* cans =
    % \/ ^* u! E. V$ ~- m6 O" w9 y     1
    . f! C, |& M) h0 T" G  S/ b. [) [- oans =1 _* d& y0 G2 R+ [3 e/ X) `1 B
         1! c& s1 z: G6 W' x- ^
    ans =
    / [$ z  |& _5 q/ H. A4 Z     1
    4 V% ^5 X* Y& L; z- m7 D. b) px =7 y* Z- Y2 U2 r: D. F
         1     2     3
      w4 \& n/ k' Gans =
    " X- S- \$ r) ~* j7 B! {& g7 S  `; M% s     0
    ( }4 v- {" F5 O2 n0 b, r2 Hans =
    9 e0 f; b; @. a- r- @' }     1
    ' K9 X# Z7 F3 G% W2 _5 C5 w$ oans =  B' H* I1 K8 ~: n
         1) u- J: P* `: J) i; ]# [0 g
    ans =
    7 G- K1 W% |: P! X0 g- L& X* l     0
    1 L, ~- S3 K, h$ [* h( Rx =7 ^. b- i; I* R# a
    Hello
    1 e7 |8 d2 f/ x7 z+ g1 V$ ^+ Vans =
    . e0 C& |$ [9 t$ d  K# L# i9 G     0
    6 C# Z0 S; s6 W6 i# cans =- b/ l0 Y4 T- P* ?, v1 k
         0, F0 T3 P9 R8 y- L9 c- L
    ans =% F% v7 i3 M8 p. h
         1! B4 A8 m+ \1 l: K; G4 k& _% D+ W
    ans =! F* U8 Z- l' X3 t8 p
         0
    : R8 x# G, U, f+ J, Q$ c& p& @8 Oans =6 i5 U8 ^! V: M3 Q' P% ]7 z- H
         0
    7 n: d" c7 e: [+ Q' m1 X: \% v: l* L$ |9 R4 Q- n/ T6 B) v
    ————————————————& n0 K1 i+ S1 Q( l2 O! \
    版权声明:本文为CSDN博主「Phil__ming」的原创文章。' E2 q, ]( J( w
    原文链接:https://blog.csdn.net/Poseidon__ming/article/details/750925751 H- F7 z9 Q2 p2 [* V5 Z0 ]8 T

    5 U8 T! [! D( L7 j4 b6 k" M; O2 U' V+ [. ?7 j
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

    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, 2026-6-12 03:59 , Processed in 0.393323 second(s), 56 queries .

    回顶部