matlab学习其一

 
-------------------------------Lesson 1--------------------------------------------
  clear %清除所有变量;
  clc %清屏;
  
1.取整函数
 round %向最接近的整数取整,小数位如果大于0.5,取绝对值大的整数
 fix %向0取整
 floor %小于等于该数的最接近整数
 ceil %大于等于该数的最接近整数
 eg.
 a1=round(2.5)  %3
 a2=round(-2.4) %-2
 a3=round(-2.5)  %-3
 b1=fix(-3.6)  %-3
 b2=fix(-3.5)  %-3
 c1=floor(4.9)  %4
 c2=floor(-4.2)  %-5
 d1=ceil(4.2)  %5
 d2=ceil(-4.4)  %-4
 
2.浮点数
  double %默认,8字节
  single %4字节
  a=single(b) %转换
  
3.复数
 使用i或者j表示虚部的的单位
 complex() 产生复数
 常用的复数函数
 complex(a,b) %a为实部 b为虚部
 real(z) %得到实部
 imag(z) %得到虚部
 abs(z) %得到复数的模
 angle(z) %得到复数的角度
 conj(z) %得到复数的共轭复数
 
4.数据显示格式
 format( ) 确定数据的显示格式
 format short 或者 format('short') 默认显示,保留小数点后4位
 format long 或者 format('long') 有效数字16位
 format long e 有效数字16位加指数3位
 format short e 有效数字5位加指数3位
 format blank 或者 format('blank') 保留小数点后2位
 format + 或者 format('+') 只给出正负
 format rational 或者 format('rational') 以分数形式表示
 fortmat hex 或者 format('hex') 以十六位进制表示
 format long g 保留15位有效数字
 format short g 保留5位有效数字
 eg.
 //这里面a和format必须换行,而且有几个不对哦
 a=3.1415926
 format short
 a
 format long
 a
 format long e
 a
 format short e
 a
 format blank
 a
 format +
 a
 format rational
 a
 format hex
 a
 format long g
 a
 format short g
 a
 
5.逻辑类型
 matlab的3种运算 : 数值计算/关系计算/逻辑计算
 logical( ) 将数值型转换为逻辑型  %可以放数组[1 2 4 5 ;1 2 3 4]
 eg.
 a1=true
 a2=false
 a3=true(3,4) %建立一个3行4列,每项都为真的矩阵
 a4=false(3) %建立一个3行3列为负的矩阵
 
6.字符串
 不区分单个或者多个,统一视为字符串
 eg.
 a='my name is Zhuge dan'
 b=char([65 66 67 68]) %字符型转换成字符串
 c=int8('hellow') %将字符串转换为整数
 d='张'
 
7.函数句柄
 (我觉得就是函数别名)
 eg.
 f1=@cos
 t=0:pi/5:pi   %从0到pi,以pi/5为间距,分别列出0, pi/5, 2*pi/5 , 3*pi/5 , 4*pi/5 , pi
 f1(t)
 f2=@complex
 f2(3,4)
 //
 eg.
 f1=@char  %函数别名
 s1=func2str(f1) %将函数句柄转换为字符串
 f2=str2func(s1) %将字符串转换为函数句柄
 functions(f1)  
 isa(f1,'function_handle') %判断逗号前后类型是否一致
 isequal(f1,f2) %判断句柄f1,f2是不是指向同一个函数
8.单元数组类型
 (类似于c语言中的结构体)
 建立方式: 1, 使用大括号{}  2,函数cell()建立
 
 (1)在获取单元数组时,可以用{}表示下标
 eg.
 c={'诸葛蛋','zhugedan';[1 2 3 4 5],100}  %逗号, 同行分割   分号;换行分割 
 c{1,1}
 c{2,1}
 c{2,2}=[]  %将第二行第二列的元素赋值为空
 
 (2)使用cell()函数创建单元数组
 eg.
 c=cell(2,3)
 c{1,1} = [1:3;3:5]
 c{2,2}='China'
 c{2,3}='Robin'
 c{2,1} =100
 
 (3)celldisp() 显示单元数组的内容
 c={eye(2),'China';[1:4],100}
 celldisp(c)
 celldisp(c,'mycell')
 
 (4)cellplot() 以图形显示单元数组
 c={'诸葛蛋','China';[1:5],100} 
 figure;
 out=cellplot(c,'legend')  %第二个参数必须为legend
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2020-02-28 20:48  诸葛蛋蛋  阅读(215)  评论(0)    收藏  举报