您的位置:首页 > 编程语言 > MATLAB

matlab -基本数据类型

2018-02-19 20:46 176 查看

整数类型

在进行整型数据之间的运算,结果为同类型的整数,不同的整数型数据之间不能进行运算;

整型数据可以和双精度标量进行数值运算,结果为整型,在运算过程中保持浮点运算精度,最后将结果转换为整型。整型数据不能与非标量双精度数组进行运算。

>> int8(240)

ans =

127
>> x=int8(240)/int8(3.7)

x =

32
>> x1=int8(240)/3.7

x1 =

34
>> uint8(23)+int16(23)
??? Error using ==> plus
Integers can only be combined with integers of
the same class, or scalar doubles.

>> 3.4*23

ans =

78.2000
>> y=int16(23)*double(2)

y =

46
>> y=int16(32)*[2.3,1.8]
??? Error using ==> times
Integers can only be combined with integers of
the same class, or scalar doubles.

>> y=double(32)*[2.3,1.8]

y =

73.6000   57.6000


mod 和 rem函数比较

mod 和 rem 函数的调用格式完全相同,均为R=fname(X,Y) (其中fname 是mod 和rem 的统称,而且当X 和Y符号相同时,两者的结果时一样的,但它们也有如下两点不同:

当Y=0时,mod(X,Y)=X,而 rem(X,Y)=NaN;

当Y~=0且X~=Y时,mod(X,Y)和Y是同号的,而rem(X,Y) 和X是不同号的。

整数操作函数

ceil 向正无穷大方向取整 isinteger 判断输入是否为整数

fix 向0取整 mod 求模数

floor 向负无穷大方向取整 rem 求余数

round 四舍五入取整

>> a=4*rand(3)-2

a =

-1.8359    1.4581   -1.4488
-0.8247   -0.2698   -1.0321
-1.8722   -1.6288   -1.1080

>> ceil(a)

ans =

-1     2    -1
0     0    -1
-1    -1    -1

>> X=magic(3)

X =

8     1     6
3     5     7
4     9     2

>> Y=5*ceil(4*rand(3)-2)

Y =

10     0     0
10     5    10
0     5     0
>> a1=mod(X,Y),a2=rem(X,Y)

a1 =

8     1     6
3     0     7
4     4     2

a2 =

8   NaN   NaN
3     0     7
NaN     4   NaN
>> z1=2+0i
z1 =

2

>> isreal(z1)

ans =

1

>> z2=complex(2);
>> isreal(z2)

ans =

0


获取复数的实部,虚部,模和幅角等信息,matlab 提供了相应操作函数

isreal 检查输入是否为实数 real 返回复数的实数

imag 返回复数的虚部 abs 求复数的幅值

angle 求复数的相位 conj 返回复数的共轭复数

>> A=magic(3)+ones(3)*i

A =

Columns 1 through 2

8.0000 + 1.0000i   1.0000 + 1.0000i
3.0000 + 1.0000i   5.0000 + 1.0000i
4.0000 + 1.0000i   9.0000 + 1.0000i

Column 3

6.0000 + 1.0000i
7.0000 + 1.0000i
2.0000 + 1.0000i

>>
>> real(A)

ans =

8     1     6
3     5     7
4     9     2
>> ia=imag(A)

ia =

1     1     1
1     1     1
1     1     1


>> s1='I love matlab' % 在一对单引号内输入


strrep 字符串查找与替换 sprintf 格式化输出数据到字符串

strcmp 字符串比较,后者忽略字母大小写 sscanf 格式化从字符串中读取数据

regexp,regexpi :匹配正则表达式,后者忽略大小写

regexprep 使用正则表达式替换字符串

regexptranslate 将私服穿转化为正则表达式

>> strrep(S,'good','bad')

ans =

I'm a bad student!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab