数学建模社区-数学中国
标题:
MATLAB数据类型
[打印本页]
作者:
zhangtt123
时间:
2020-1-20 16:59
标题:
MATLAB数据类型
MATLAB 并不需要任何类型的声明或维度报表。 MATLAB 每当遇到一个新的变量名称,创建变量,并分配适当的内存空间。
# S7 Z, ^3 _1 ] e
8 E' M- X6 w4 L0 ]
如果变量已经存在,则MATLAB替换以新的内容的原始内容,并分配新的存储空间,在必要的情况下。
4 g* Y/ P# ^! f
1 ?+ f% ~) a7 @* D m8 g I
例如,
' ?, `) c7 Q/ p3 U
+ M% m! @* \# A. Z# `) F
Total = 42
i( b- k! ?4 J; z$ x7 k
上述语句创建了一个名为“Total” 的 1-1 矩阵存储值42。
/ S: P6 l4 S! ^6 W7 U. b# x; ^% `
0 x) I6 O; f; l6 I X6 v
MATLAB中可用的数据类型
8 J B7 q! A" D4 F0 x
, C8 p1 o H0 I' n) a7 b/ W6 e* E
MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。
# b3 z, ^, b, W: \
, g5 p8 e, u s$ C
下表显示了在 MATLAB 中最常用的数据类型:
R4 M; D/ x) S& G
5 k1 f' i" N# M7 `9 E) Y* o
数据类型 描述
, V" U7 I5 l4 e) W
int8 8-bit signed integer
0 m/ |8 U/ C/ K6 E/ m9 S, u# D$ a
uint8 8-bit unsigned integer
* E! e# p1 X9 r8 f% g8 J* y
int16 16-bit signed integer
- M/ H1 M1 z3 }2 R3 ]
uint16 16-bit unsigned integer
; c0 G3 C0 S* n$ v
int32 32-bit signed integer
8 |! O) n5 g, C# {. p
uint32 32-bit unsigned integer
8 n7 f% M: X) \2 b# Z+ V j3 h% c
int64 64-bit signed integer
6 J% T. a; Y# |" i0 S8 z
uint64 64-bit unsigned integer
0 _; s8 G, L5 f1 q( x
single single precision numerical data
. \6 Q. Y8 j |% T$ y& w `% Y
double double precision numerical data
. @5 E4 D8 o) c: N, f4 d+ b
logical logical values of 1 or 0, represent true and false respectively
) I& m& |5 M4 i4 Y9 ]8 F
char character data (strings are stored as vector of characters)
5 ]; S- @, H6 Q! e. h" X) D5 ^2 j
cell array array of indexed cells, each capable of storing an array of a different dimension and data type
3 f* u, C+ U( R+ [8 D
structure C-like structures, each structure having named fields capable of storing an array of a different dimension and data type
0 l8 Q! ?# C* J) u4 l8 C- L; X
function handle yiibaier to a function
2 w- |$ n5 s5 ]7 V- z
user classes objects constructed from a user-defined class
7 b; s- d2 ~" E( T$ V
java classes objects constructed from a Java class
/ W8 x/ o) ?9 G! Q( M
例子
! m2 A9 d3 o6 U/ D# ]& `8 U8 M
, k) `1 j% z) a
创建一个脚本文件,用下面的代码:
3 d2 E u& }2 {6 u! O) P4 F( |. [
1 R) V* `3 r+ h* @& w% X1 J
str = 'Hello World!'
2 c2 w5 p( Q: C% z4 ~$ `
n = 2345
$ B: _+ x! n' X# C
d = double(n)
* r2 ]) }& B# ]* `2 I
un = uint32(789.50)
/ E" h4 @( b4 Y& o
rn = 5678.92347
8 |! @6 s( q4 W( Q* Q- a
c = int32(rn)
) B+ d% |. S& r, m& g4 O' J& J! `
上面的代码编译和执行时,它会产生以下结果:
" g2 \4 p, a5 }8 q3 w
2 y5 ~) I5 D/ x$ T6 z% c
str =
* w3 w D# R1 `3 J0 Q
Hello World!
9 p' O5 S! V. s0 \6 [/ m
n =
l F+ m/ i; c1 K* R/ F q
2345
, q) A5 R. S+ f; d/ {- j
d =
' C2 T+ O/ A8 i8 {. `
2345
' P. h6 e% M! A7 i! t j2 C
un =
7 _6 b% x9 f- O3 Z$ i3 J Y
790
+ Q( u9 L; Q* R' A* h+ i9 M
rn =
) m" w9 c+ N( J# Z: h. s
5.6789e+03
4 E& S" ^# q3 H/ k6 S
c =
( L* r E7 a/ z! P
5679
+ }. z0 U( N. m" X& K, ?0 e* k
数据类型转换
5 u' d. L3 E: m/ E3 Z' `. G
% Z) M) s( S$ p1 H
MATLAB 提供各种函数,用于从一种数据类型转换到另一种。下表显示的数据类型转换函数:
Y- h9 p9 v% N5 N& ]
2 O1 V4 d, _8 I
函数 目的/作用
2 A, W7 d6 `; A( @+ Z, G+ _: D: B
char Convert to character array (string)
7 i+ [ f. [7 i |' s4 \! J# j
int2str Convert integer data to string
2 p& {& }' x, A3 }1 I
mat2str Convert matrix to string
7 C* B* Q/ l g; a' O
num2str Convert number to string
* s7 F+ T2 ]2 A/ o- K" H
str2double Convert string to double-precision value
3 H$ e& }3 C9 S! i3 u$ g0 i. z
str2num Convert string to number
9 z& j9 s# s* L" ]& |
native2unicode Convert numeric bytes to Unicode characters
5 M# @2 y8 S5 _ p
unicode2native Convert Unicode characters to numeric bytes
% J- `# s4 f) E$ V9 Q
base2dec Convert base N number string to decimal number
. ?* w! J* V; C2 E @
bin2dec Convert binary number string to decimal number
2 T8 b& ], s" m# n3 b! A: l
dec2base Convert decimal to base N number in string
) Q: N- W. e; p! \- |7 A' ^ w2 P
dec2bin Convert decimal to binary number in string
2 z/ H1 u ^7 }
dec2hex Convert decimal to hexadecimal number in string
, h1 \7 u, @1 @# c
hex2dec Convert hexadecimal number string to decimal number
, F+ [% r9 V+ H
hex2num Convert hexadecimal number string to double-precision number
K# D- _' {0 f& K
num2hex Convert singles and doubles to IEEE hexadecimal strings
' o0 q+ X, v. o) m- s$ I+ i
cell2mat Convert cell array to numeric array
# \6 u; N3 z; D( f
cell2struct Convert cell array to structure array
4 p1 [$ ]: c. G, D. w
cellstr Create cell array of strings from character array
; W$ {: \$ F: C: L
mat2cell Convert array to cell array with potentially different sized cells
+ a+ t1 }& d: E) T
num2cell Convert array to cell array with consistently sized cells
" }; X$ H4 U+ c" q2 l) r+ V5 |
struct2cell Convert structure to cell array
- z/ e/ ^ c$ o. ]7 [3 Z
测定的数据类型
" [1 q/ {- a9 f, E1 i- P
/ ]0 i* E8 N; g/ A- N
MATLAB 提供各种函数标识数据类型的变量。
- `# n- F( a6 F' H
. k3 V5 ] U! l; F; Z7 v0 z
下表提供了确定一个变量的数据类型的函数:
& @$ @* O$ z8 G% Y6 c7 E
8 C! E r( s y% N* M; f/ l/ q6 ?9 R
函数 目的/作用
8 ~: Q7 Z# Z% }) N. e/ V
is Detect state
" u/ T, U8 B W! o* m
isa Determine if input is object of specified class
" y4 a# |9 @3 j7 E2 D: ~
iscell Determine whether input is cell array
6 i# V) c, h$ P& n" J8 c+ ^6 ?
iscellstr Determine whether input is cell array of strings
8 t* G2 Y8 _. {& D* ]7 f k6 t
ischar Determine whether item is character array
( k+ Z) x. ~. \* s0 H0 v4 m6 `4 p
isfield Determine whether input is structure array field
; b7 \7 ^- d+ g/ }+ G W- ?
isfloat Determine if input is floating-yiibai array
5 Y* A+ k1 B* C9 t/ B
ishghandle True for Handle Graphics object handles
' ?6 K2 K: b! u
isinteger Determine if input is integer array
' g$ a- t. C0 g* z6 h* J& A
isjava Determine if input is Java object
* t% _/ Q( n+ F* H: Y/ |
islogical Determine if input is logical array
( { k* {% {" F) M. A) \
isnumeric Determine if input is numeric array
! i& @% F6 J& B8 Z5 X
isobject Determine if input is MATLAB object
* R' u w8 e3 H3 u& q `/ \, _
isreal Check if input is real array
- f% f1 t5 v" U% A4 w. ]
isscalar Determine whether input is scalar
3 [9 t; q/ Y% Q" c M
isstr Determine whether input is character array
/ n; k* O' y3 r$ Y: _0 a
isstruct Determine whether input is structure array
) m |# R# a ?. N* y
isvector Determine whether input is vector
i' L( k, x- z4 {
class Determine class of object
% x0 _4 }8 T, _" F E
validateattributes Check validity of array
2 l( h, {6 `% ^ X% P# a
whos List variables in workspace, with sizes and types
) P4 u$ H U9 w5 T/ k7 x* E% |4 B
例子
U5 s o$ [5 b& L: i: B
' b9 c& Z/ s7 B; w. g6 S
创建一个脚本文件,用下面的代码:
0 k0 B8 O0 L m; H3 {
' c j2 H/ \# P- X5 R
x = 3
- Z5 g/ \6 e( o* q. E- \% X0 a3 f
isinteger(x)
: A* m2 t1 M' l! \
isfloat(x)
% [6 m y+ N* f6 ?# q; O- H7 m3 [
isvector(x)
! N4 J l" s3 X# U% b
isscalar(x)
- R# ~# b) S$ S# [5 K! ?
isnumeric(x)
" @1 g; ]9 j1 p& }! ]7 P
; p8 k/ e; {5 y/ `& X1 r7 q- d3 [
x = 23.54
) y0 F# f% o# x1 V
isinteger(x)
9 x- S9 I' }3 d' j, ?
isfloat(x)
: x$ w: A! r$ h, W; O$ [5 |
isvector(x)
* Y3 a8 }' Z, i) ?- s
isscalar(x)
( Y( }" F0 A) v9 ]
isnumeric(x)
8 }9 \" V2 [6 y# T1 ~0 I" \1 b
& @* x9 m5 e7 R' o6 }' D6 W
x = [1 2 3]
4 H9 E' N) N8 Q9 w# `9 {. O
isinteger(x)
( |8 z3 h/ Y2 t3 r' ^
isfloat(x)
. y, T: R; B! k; A6 R
isvector(x)
2 U, y P' R7 B2 B& E5 U2 b
isscalar(x)
9 d: ~0 U5 X) }5 c& B" k) Z0 l
" X# A( D3 y* `2 G0 v; M) R
x = 'Hello'
% C% n2 E$ Z; d& Z! O- u/ Z
isinteger(x)
+ N. Q& V \) r5 C3 y9 F
isfloat(x)
/ S6 \" d* y1 d3 {4 q. v9 T
isvector(x)
" H6 Y, T) y. t1 v. s9 }* {
isscalar(x)
1 y* D+ c1 P0 j& ?* f" K* {+ H. ]
isnumeric(x)
h; ~' J; M, ^2 A! q7 E. E- }* L
当运行该文件,它会产生以下结果:
3 ^1 y6 g1 B4 }, [% }! C
+ X" c: ]( @) A2 t( y! v9 @$ \8 y
x =
' y: ?; h: ?2 z4 y2 R6 N
3
9 P3 P& V+ t7 G0 R7 @/ C0 A. b! F K$ U
ans =
. p- I* F; D) y6 [: v
0
0 m' D! p) s) S; b& p
ans =
1 {4 u1 S* R/ l+ I( ?
1
1 l; l5 y9 P5 j; B6 o4 l
ans =
0 ^3 G$ A F$ c) t6 L1 s% o# Z
1
# a$ u% l8 `/ P4 a$ O9 S! L
ans =
( h1 X/ H. L: ?+ J3 m8 g
1
3 ~. @$ L. X2 c
ans =
2 P" s, n& \$ a. d& i# E
1
1 O8 l: T. i$ R7 W4 y+ M
x =
, p1 ?% E2 }% b7 g0 ^) q
23.5400
* R9 S, w5 p }5 S5 R ? d% a
ans =
& F# P. {5 ^( s1 k
0
8 c& O1 p0 j. L
ans =
- X; J0 M, S$ C- A& w
1
7 l5 V' k) t# l+ a9 ?& ?9 P
ans =
. R$ a0 P/ N p5 Z9 s8 |' [
1
6 I* w4 A( z) U8 G6 t; k+ l* `
ans =
) c5 B: ]) h" [! S- a U
1
9 d# i! K0 I9 [/ n/ w0 Y( l# N
ans =
' r* r2 A" k2 \6 y
1
: ^8 |' {# v: p) ~2 H
x =
3 P+ J3 b, P- M$ y1 y$ o6 Q
1 2 3
/ n- V1 m* E$ s) v
ans =
+ i, l: g! j; [! I W1 V
0
+ h" r5 C# t/ R+ a' Z8 n3 P2 N5 I9 v
ans =
! c/ V/ i& Q( @9 L
1
% `$ S; V& ]) |, D
ans =
- X# Q3 h, l! D- E3 @0 [
1
3 N1 X2 ?6 B/ z1 l, r
ans =
& V' l1 R. u# s6 t- ]) c1 c( t
0
2 g4 I$ e: [8 m( L0 ~
x =
5 l+ A" z, l: r4 k7 V: b4 X
Hello
2 [* j5 c# _& E" ?+ W1 ~8 P/ k, T+ I @
ans =
4 h9 U& y- \8 c/ S9 @: U# B, w
0
8 ~- w, E1 x- T( N
ans =
& U, W7 }" n* m+ J2 A4 i
0
& x4 ?7 r. t6 L/ O$ o
ans =
' |6 O9 k3 w1 x3 W( u
1
3 J! d/ \2 |, a- }- Z- [, {! h
ans =
4 `$ S, K. J6 K
0
. E$ h9 J; u# _/ l; C9 }) w
ans =
7 M; |% ` r, U+ Q) R# s; n
0
3 k! `* `* [* w' D% ^
0 m0 a% b1 @, |/ P4 N% U
————————————————
' [; P# M8 e% n2 M, M
版权声明:本文为CSDN博主「Phil__ming」的原创文章。
, G" S" T2 v) H/ Z7 b& g
原文链接:https://blog.csdn.net/Poseidon__ming/article/details/75092575
9 v7 m) [6 M: l; f
. R; \" d1 g) V4 e i
, D( W" o4 ~% d% V @
作者:
18019574874
时间:
2020-2-4 15:58
发表回复赞
# R# @, x9 z, t
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5