Matlab学习日记(三)

本文详细介绍了MATLAB中的变量赋值、数据类型以及运算操作。内容包括不同精度的浮点数(double和single)、整数类型(int8, int16)的范围,以及如何显示和改变精度。此外,还讨论了字符串、字符、逻辑运算以及随机数的生成。通过示例展示了逻辑运算符(如`<`, `>`, `xor`等)的使用,并探讨了运算优先级。最后,文章提到了一些特殊数值如Inf和NaN,以及如何生成随机数。
摘要由CSDN通过智能技术生成

3.1 变量赋值和变量类型

who可以输出已经有的变量

whos可以输出变量的大小(1*1)、比特(空间)、数据类型

清除选定的3个变量命令:clear a b mynum

double是64个比特,存储2的64次方个浮点数(有小数点的数),支持15-16位的小数,双精度

single是32个比特,存储小数7-8位,单精度

>>format long %显示最长的精度
>>pi
ans =
   3.141592653589793
>> pi_double=pi
pi_double =
   3.141592653589793
>> pi_single=single(pi) %单精度输出
pi_single =
  single %当double是没显示是因为matlab默认浮点数以双精度存储
   3.1415927
>>integer_14_int8=int8(14)%整数变量存储为14,在int8这个形式
integer_14_int8 =
  int8%形式是int8
   14%值是14
>> integer_14_int16=int16(14)
integer_14_int16 =
  int16
   14
>> intmax('int8')
ans =
  int8
   127%int8中能赋予的最大值是127,2的7次方-1 
>> intmin('int8')
ans =
  int8
   -128%int8中能赋予的最小值是-128,2的7次方的负数
>> intmax('int16')
ans =
  int16
   32767%2的15次方-1
>> intmin('int16')%int16会有16位比特存储数字,1位比特存储正负号,15位存储数的绝对值
ans =
  int16
   -32768
>> intmin('uint8')%unsigned即不带符号的
ans =
  uint8
   0
>> intmax('uint8')%
ans =
  uint8
   255

(能用到的几乎所有的数据类型)

 

>> Apple_char='Apple'%让Apple以字符的形式存储在Apple_char中
Apple_char =
    'Apple'
>> Apple_string="Apple"%让Apple以字符串的形式存储在Apple_string中
Apple_string = 
    "Apple"
>> whos
  Name              Size            Bytes  Class     Attributes
  Apple_char        1x5                10  char                
  Apple_string      1x1               158  string              
  ans               1x5                10  char  
>> char('Apple')
ans =
    'Apple'
>> double('Apple')
ans =
    65   112   112   108   101 
  
>> logical_true=true%逻辑数据类型
logical_true =
  logical
   1
>> logical_false=false
logical_false =
  logical
   0
>> whos
  Name               Size            Bytes  Class      Attributes
  logical_false      1x1                 1  logical              
  logical_true       1x1                 1  logical   

3.2 运算优先级和内部函数

>> iter=10
iter =
    10
>> iter=sqrt(iter+1)%sqrt是'square root'开平方根
iter =
   3.316624790355400
>> format compact%使用后,空行就没了
>> iter=sqrt(iter+1)%sqrt是'square root'开平方根
iter =
   1.754322906758696

3e8是3乘以10的8次方 e可以是E,D,d

format四种用法

format long是小数点最长

format short 输出四位小数

format compact是去除多余空行

format loose 空行回来,输出结果松散

abs(-5)输出5,是绝对值的意思

>> help elfun% 函数帮助
  Elementary math functions.

3.3 常数和随机数

尽量不要设置以函数名为名的变量,会引起错误

i和j是虚数,但是经常用作循环的变量,这时,它们作为虚数的功能就消失了

>> i
ans =
  0.000000000000000 + 1.000000000000000i
>> 2+3i
ans =
  2.000000000000000 + 3.000000000000000i
>> i^2
ans =
    -1
>> 1i
ans =
  0.000000000000000 + 1.000000000000000i
  
  %% 正无穷负无穷
>> Inf
ans =
   Inf
>> Inf_double=Inf
Inf_double =
   Inf
>> -Inf
ans =
  -Inf
>> 1/0 %正无穷
ans =
   Inf
   
%%   
>> NaN %not a number
ans =
   NaN
>> 0/0
ans =
   NaN
>> nan_double=NaN
nan_double =
   NaN
>> whos
  Name            Size            Bytes  Class     Attributes
​
  Inf_double      1x1                 8  double                          
  nan_double      1x1                 8  double   

随机数rand()、randn、randi

>> rng(1)%rng='rand.num.generator'随机数生成器,让种子是1
>> rand
ans =
   0.417022004702574
>> rand
ans =
   0.720324493442158
>> rand
ans =
     1.143748173448866e-04
>> rng('shuffle')%产生随机数列,是一个不会重复的随即因子.用时间作为随即因子的,不会重复
>> rand
ans =
   0.213360561985271
>> rand
ans =
   0.927070783266033
>> rand()*2 %产生0-2之间的随机数
ans =
   1.292064407270423
>> randn %n=normal distribution 产生正态分布的随机数,均值是0,方差是1
ans =
   0.966668170039448
>> randi(12) %1-12的数
ans =
     1
>> randi(12) %1-12的数
ans =
     8
>> randi([4,12])%4-12的随机整数
ans =
     5
​
%%练习题
>> 100*rand %产生0-100的随机数
ans =
  99.649233861273217
>> 15*rand+20 %产生20-35的随机数
ans =
  25.035596686977584
>> randi([1,100]) %产生1-100的随机整数
ans =
    49
>> randi([20,35]) %产生20-35的随机整数
ans =
    34
    
%%字符的代码
>> double('a')
ans =
    97
>> double('b')
ans =
    98
>> char(97) %将97的代码转化成字符
ans =
    'a'
>> char(1:128) %会出现所有的字符
ans =
    '•••••• 
     ••
     •••••••••••••••••• !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~••'

3.4 逻辑运算+字体设置

>> 3<5%逻辑变量1,是正确的
ans =
  logical
   1
>> 3>5
ans =
  logical
   0
>> true
ans =
  logical
   1
>> false
ans =
  logical
   0
>> xor(3<5,1<3) %当且仅当括号里的有一个正确的时候返回1,否则是0
ans =
  logical
   0
>> xor(3<5,1>3)
ans =
  logical
   1
>> 'b'<'a'+1
ans =
  logical
   0
>> 'b'=='a'+1
ans =
  logical
   1
>> 10>5>2
ans =
  logical
   0
>> 10>5&&5>2
ans =
  logical
   1

~=是不等于,==是等于,&&是且的意思,||是或的意思。~(3<1)是先算括号再否定,返回结果是1

单个的&和|针对向量和数组而言的

优先级

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值