QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3244|回复: 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 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
    ( z: U6 }, u" e
    2 Y) i4 K0 a7 d/ e如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。2 i/ U$ d' ]4 Q; G! t

    . O- B; P( x: r# o5 e% \4 [" m0 n例如,
    ; p7 i' P& O& k! b
    ' |; N8 p# A9 B+ P8 g: p5 w4 gTotal = 42" d+ \! D! U: Q" A
    上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。
    5 z; J; ^7 u0 [  t) H! L. S* }/ u/ c$ A2 l, j
    MATLAB中可用的数据类型% r* S4 K8 G& \8 l* e6 O
    ; ]# Q6 ^3 h7 F* H
    MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
    5 \( o* m0 O; r* g9 I$ X) q; _" j5 Q5 ^
    下表显示了在 MATLAB 中最常用的数据类型:/ L( W9 Q) g' C  G* f
    1 d# c7 |; d5 B" D
    数据类型        描述9 G: ~- n- `7 t+ b& M4 A
    int8        8-bit signed integer0 \5 ~7 _: H" N( @# ~
    uint8        8-bit unsigned integer$ P; E# Z: ]: O6 V
    int16        16-bit signed integer
      x" r7 c0 |2 p+ ~  q, [$ duint16        16-bit unsigned integer
    ) a: q. r. q/ T# ]  U' E8 U2 eint32        32-bit signed integer' s  F" I3 {* H9 i+ X7 r5 }) c3 O
    uint32        32-bit unsigned integer- i& f) b$ D0 @& Q. U6 Z) q5 `; u' v
    int64        64-bit signed integer
    2 N+ y' F& P/ e8 \7 Z- Luint64        64-bit unsigned integer
    ! \! ^4 s0 @: C  Xsingle        single precision numerical data  u, _, c: a/ m. K' t+ y+ H. y0 I
    double        double precision numerical data
    ) g) A5 f5 Y) w. S3 zlogical        logical values of 1 or 0, represent true and false respectively9 X% {- \- O) ]8 D; c
    char        character data (strings are stored as vector of characters)
      i  S! Z5 _/ L- L: n, X$ ocell array        array of indexed cells, each capable of storing an array of a different dimension and data type+ K! _  w9 O, S; ~
    structure        C-like structures, each structure having named fields capable of storing an array of a different dimension and data type% F& ~% a3 e0 Q& H2 J8 w8 T, L
    function handle        yiibaier to a function
    4 o4 R; ^* r$ f1 ?( e4 K; t5 Huser classes        objects constructed from a user-defined class
    - Y. o( @2 b% \; w5 C8 _* Y$ Ejava classes        objects constructed from a Java class! Q' w# D7 N# o4 Z7 L4 T& V1 x
    例子  r. p$ B& D$ l1 U6 {

    % M" P) y  Y$ v创建一个脚本文件,用下面的代码:3 l3 g) |& W% W# M& {; X

    - t+ A, \! ^7 i str = 'Hello World!'& W6 r1 p. M" C  A8 b7 {
    n = 2345
    - q* W  w  q, z  ]- ~d = double(n)) z. z, _$ K& `) G$ Q& I) T* N) @
    un = uint32(789.50)
    5 |  N) M# f" d. G% }7 Q' Arn = 5678.92347  T0 n" ~  s! j. f2 y1 O- I
    c = int32(rn)
    & S; L! d' Q4 a: J  S' m& @& t4 t% w上面的代码编译和执行时,它会产生以下结果:* }% {" O+ _3 `! U8 @, u0 {+ Q

    # X1 O" [1 X" r" gstr =
    7 r' x5 Z+ |, H; l0 nHello World!
    ) ]) |  ?& V4 X% E6 bn =6 I0 z( n  D; w" `; e
       2345
    * ?! P$ A* `  R4 e* Id =  |3 L+ j" D+ H$ M$ s- S+ [
       2345' g+ o) h( T0 r2 n* O9 X$ Z
    un =' I+ V( R* y% m+ L' S: q) U+ }
       790
    2 R+ G3 r4 g& Z* ?) W* ?' l7 yrn =
    - @: f  l: @& N( j9 ^5 {" j. F   5.6789e+03" K% d' s- h2 I1 z
    c =
    0 V  D1 K& T& x, }) R; X   5679. q+ z! O3 B0 ]+ L
    数据类型转换6 A5 r1 a+ }* Q  v5 e' Q

    $ c; S* g9 r" sMATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:8 G+ C/ x8 z! l& r# A

    , Z7 T% l& P& V* f函数        目的/作用/ [2 n% f0 k* {  j% e. O
    char        Convert to character array (string); ^- b7 [2 }2 e- n5 U2 Y3 i4 p
    int2str        Convert integer data to string
    7 `" t# n7 S' i% S3 e6 u, pmat2str        Convert matrix to string
    3 x; F1 Y% x; l; E4 H5 ?num2str        Convert number to string
    6 h- B. `) V" astr2double        Convert string to double-precision value1 W% J* j/ f  d2 K
    str2num        Convert string to number
    % ?, k+ A* G+ X0 Onative2unicode        Convert numeric bytes to Unicode characters/ \9 v6 }! V+ M: r! @
    unicode2native        Convert Unicode characters to numeric bytes- V! N3 s$ t3 B( I) u" {3 N
    base2dec        Convert base N number string to decimal number! m; v- R, z1 x* _9 O
    bin2dec        Convert binary number string to decimal number
    , o3 i! q# S) w7 r4 ]& Y( d6 udec2base        Convert decimal to base N number in string
    $ H7 b- E3 G: O2 q7 Z5 D7 Fdec2bin        Convert decimal to binary number in string
    7 Z$ }6 ~: u+ kdec2hex        Convert decimal to hexadecimal number in string
    3 a2 S$ B5 @2 Z+ e! q: z! M/ Whex2dec        Convert hexadecimal number string to decimal number- S! }0 C0 B% q
    hex2num        Convert hexadecimal number string to double-precision number) R0 }8 u: H* A- F
    num2hex        Convert singles and doubles to IEEE hexadecimal strings
    & G" C! `6 `7 i% d0 l8 z) p% D& {cell2mat        Convert cell array to numeric array4 h& [5 x( J. Z
    cell2struct        Convert cell array to structure array
    2 \/ `( ^$ g8 G3 d$ _cellstr        Create cell array of strings from character array, ?6 A& e2 @) N/ h7 Y
    mat2cell        Convert array to cell array with potentially different sized cells$ _. ^1 l' h4 e9 @0 q$ z/ N# `) ]
    num2cell        Convert array to cell array with consistently sized cells
    " V  K9 i) v( v- hstruct2cell        Convert structure to cell array. E. o, C0 _0 n2 Q: G
    测定的数据类型
    : D  E  g. b9 c  W7 l
    6 k/ P5 |' |" f  `2 B; T+ _MATLAB 提供各种函数标识数据类型的变量。' f2 A" }+ l3 E& B

    4 l7 n9 L" m7 O5 l, r下表提供了确定一个变量的数据类型的函数:
    , J9 V7 \3 ^$ P, K( t5 B0 U- ]- n* K% m- v
    函数        目的/作用
    / E0 @5 r7 u$ j- wis        Detect state
    * j& S8 p* m  G- K2 [/ J3 Lisa        Determine if input is object of specified class
    0 g: c& J2 Y5 l4 {  |7 k9 uiscell        Determine whether input is cell array
    ' t* S8 i+ L/ n5 W3 Niscellstr        Determine whether input is cell array of strings8 y! u" Y3 d7 I7 D% G% Z/ H0 m
    ischar        Determine whether item is character array
    - T4 \7 u! O8 c9 d8 V: Aisfield        Determine whether input is structure array field6 {) N8 N# c- R: _2 C
    isfloat        Determine if input is floating-yiibai array& r; ?/ P' A  U( O8 @3 t) \
    ishghandle        True for Handle Graphics object handles; y1 `+ P8 O; A& P
    isinteger        Determine if input is integer array
    ( b) U' J% p- r& gisjava        Determine if input is Java object2 z% F1 `5 e- R
    islogical        Determine if input is logical array6 \4 X6 N: j) s8 ]
    isnumeric        Determine if input is numeric array
    / s/ L* z- a; W; |  J/ d+ H* Cisobject        Determine if input is MATLAB object1 d. b* \% k4 I8 i! x7 G0 k
    isreal        Check if input is real array
    4 V2 V- O# T$ i+ h0 n" Q. g5 X, aisscalar        Determine whether input is scalar
    : s1 T, _6 s$ E+ K! Oisstr        Determine whether input is character array
    # c5 _. O' o1 Y& ]: ~$ j7 misstruct        Determine whether input is structure array+ `0 x$ H( @6 {! D( I
    isvector        Determine whether input is vector
    + F% i1 S7 Z+ o" p+ K8 }/ xclass        Determine class of object
    6 l7 n$ n' n8 p1 G8 }validateattributes        Check validity of array" n1 I$ r6 f9 b: R3 B" I; u
    whos        List variables in workspace, with sizes and types2 _# R1 @+ v) E8 Z0 p) q3 D  }
    例子+ ~2 Q7 M; J( p4 p, R  G

    - }' ?2 k. O4 A) f- z创建一个脚本文件,用下面的代码:
    5 K. |7 D! D0 Y" f2 u
    8 A" p' T8 t# \: ?x = 3% H4 W0 ]0 F$ T* W& |/ v
    isinteger(x)
    . x: m8 ^3 U7 D! B; U7 Z6 gisfloat(x)
    % A& e4 J( }5 i7 E9 ^$ R/ Bisvector(x)
    ! r# _: x2 h9 r0 b  E9 l/ |* aisscalar(x)2 b$ ^- O/ f8 ^$ A3 u4 B4 B2 O0 T3 G
    isnumeric(x)
    ) J# `. i( E9 Z, d5 r; m, y/ `7 |7 U
    x = 23.54
    3 M& Q7 j7 U9 V7 F; l2 [isinteger(x)
    ; \# @. E- Q8 G' w! C1 I' f: iisfloat(x)4 H4 y2 G) f) X; i
    isvector(x)0 G$ R. C9 U8 k7 S& A4 T: r) k, d
    isscalar(x)1 \) T- @& a  E/ W
    isnumeric(x)' E* n: R+ @* I  T, ]2 S1 _

    + K, L# u- ~! J3 ]) w& Zx = [1 2 3]: s; h* n' O+ l* C, A) e
    isinteger(x)
    ' z- W- L% D$ U, N! Lisfloat(x)" u8 Q% |: m1 S- C3 Y' `! }/ D
    isvector(x)
    . h1 m6 S$ C6 r2 ^  }7 P. Qisscalar(x)
    4 o; l7 o0 O  ?, I5 W) Y3 g/ Q) M; l7 s9 r7 O) a/ Y
    x = 'Hello'4 g2 C% e7 |' U1 Y% G3 g
    isinteger(x)
    / p5 B9 ~9 d+ ~# e) S, S( Z) }isfloat(x)) M5 o8 s8 J0 D1 B
    isvector(x)
    ) l3 ^, d% _0 Q0 a8 Q7 u& Disscalar(x)" i7 @8 r9 M5 a
    isnumeric(x)
    ! p# G4 A8 _& \. T2 A, W$ B当运行该文件,它会产生以下结果:
    5 \1 h9 q  K9 N/ J9 O' \' R8 n0 B
    9 _  M/ s2 D8 X: X. e( Wx =
    ) U1 G+ c2 D+ k; j5 C     3
    # ^5 }4 H/ ~: [: k. tans =  e# C4 y) ?7 q
         0/ y! T# S1 \, ~' P) G1 ^) r
    ans =8 G2 u2 v+ Y2 u/ v) r0 U
         1: T- ?; f& D2 L9 \
    ans =
    & q1 V# Q$ ~! ]     1- F5 ~/ [: @0 U; |4 I( @2 I
    ans =
    3 c+ |* [0 O- ^7 k     1( g) X$ V/ _( ~& a$ G" Q
    ans =  ?' s( e! y; a; f
         1$ n( i, K. Y6 ]3 O
    x =  ^- i+ @& m9 u; B; z1 b& k
       23.5400
    6 u2 D% ~5 _6 R. t. Nans =4 ?$ S6 l# k3 H' Z8 R8 F" |' W
         0& W4 J& S8 d+ h: i; N0 o1 y/ h
    ans =5 E# `8 c0 t  p- n
         18 s( O/ [* \8 |& g! l" f; g
    ans =3 W- I9 R' \; T
         1: A4 e7 e: I3 _! T
    ans =
    # Z0 r. Y) a# e: F0 a* j     1
    6 Q6 `" \6 c. F& W* @ans =
    ! v9 x; e: z2 Q& G1 ]% v0 l+ f' Q     1
    : K( V: N( X2 {- C+ m+ t  D( Dx =% n" z0 s3 l1 V$ S& |7 e
         1     2     3' |2 B! j5 j; O, Y* x6 \
    ans =- Q. q% W* \; t7 ]( R# a
         0
      F  ^8 ~2 ]+ f4 D5 tans =2 }( Q2 j; [+ g5 i- B
         15 l9 B" ~( a1 [% O, o
    ans =
      V4 B, ^0 ~1 Q: P% [& Q- e1 ]     1
    % E: b) w) Y- v* L) S( V/ T* Eans =! ]: {9 L% H4 Q( E* _
         0* g: B2 z- t4 A. R: {
    x =5 E8 r1 |, e4 X# c
    Hello/ p3 N" Y) f* h4 w, L- [0 e
    ans =2 @" V0 D; u+ ~+ Q! x
         0' H* R# O; E8 N) g7 b( [
    ans =
    & K1 k+ Q' L$ R% }     0: B" N( E' ~! x# L( I9 X- i
    ans =
    0 w; M7 b% P9 Q6 Y# }     16 G7 b% G: @0 J( A' O/ Q
    ans =0 n7 U5 x* |+ y
         0
    . F! F1 h6 ?) L2 ]' y- aans =
    # u1 s2 ^  M6 N+ y8 _1 O: J1 \     0
    ' p2 Y, S6 \- w5 @( Q1 n' z0 ^  G8 q# ?+ L: e4 ?8 h! l/ V
    ————————————————  J6 O8 @' a, }! k/ [' L
    版权声明:本文为CSDN博主「Phil__ming」的原创文章。6 G$ \4 q! J+ j8 S: K
    原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575# p. m2 _& `; G: G3 D; V" n

    2 v! D* T; N# B/ V6 y. k. ]3 h$ d$ b0 \0 u6 R& j7 [
    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, 2025-11-26 17:53 , Processed in 0.392250 second(s), 56 queries .

    回顶部