广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    matlab常用函数之format函数short/long/shorE等

    来源:网络收集  点击:  时间:2024-07-29
    【导读】:
    Matlab的名称来源于“矩阵实验室”,其对矩阵的操作具有先天性的优势(特别是相对于C语言的数组来说),被广泛的应用于科学计算,包括数值微积分、复杂系统的仿真,线性/非线性拟合等等举不胜举。对于初学者,往往对一些矩阵的简单的操作不知道对应的函数是什么,有可能费很大的周折,其实matlab提供了一大堆对矩阵的操作本系列文章旨在对一些Matlab中常用的函数进行介绍。工具/原料morematlab方法/步骤1/11分步阅读

    matlab中format函数用于控制matlab命令窗口中输出结果的显示方式和位数。format的调用形式为:

    formatformat typeformat(type)

    其中第一个表示采用默认值,后面两种的type为具体的显示类型字符串。matlab提供了十几种type,包括short (默认),long,shertE,longE,shortG,longG,shortEng,longEng,+,bank,hex,rat,compact,loose. 注意这些type不分大小写,比图short可以是Short,sHort或SHORT等,format内部会自行进行转换识别。

    可以用get(0,FormatSpacing)来查看当前是compact还是loose或者用get(0,Format)来查看当前的其他形式。下面进行一一举例说明。比如执行下面的代码:

    clc

    format compact

    get(0,FormatSpacing)

    format loose

    get(0,FormatSpacing)

    format short

    get(0,format)

    format long

    get(0,format)

    2/11

    首先来看compact和loose,这两个与其他形式不一样,只控制输出结果显示的间隔,不影响数据显示的位数和精度。用get(0,FormatSpacing)可以显示当前是compact还是loose.执行下面的代码:

    clc

    format compact

    A1=100

    A2=pi

    A3=rand(3,4)

    format(loose) %等价于format loose

    A4=100

    A5=pi

    A6=rand(3,4)

    可以看出A1 A2 A3之间没有空行,是以compact形式显示,而A3 A4 A5 A6间都有一行空行,以loose形式显示。

    3/11

    下面是short形式,这是matlab的默认显示形式,使matlab以short形式显示的调用方式有

    format

    format default

    format(default)

    format short

    format(short)

    5种。matlab帮助文档对short的解释为:Scaled fixed-point format, with 4 digits after the decimalpoint. For example, 3.1416.If youare displaying a matrix with a wide range of values, consider using shortG. 简单理解就是保留4位小数。

    执行下面的代码:

    clc

    rng(default)

    format short

    a1=pi

    a2=1/3

    a3=rand(3,4)

    a4=eps

    a5=realmax

    a6=realmin

    a7=intmax(int8)

    a8=intmin(int8)

    a9=intmax(int16)

    a10=intmin(int16)

    a11=intmax(int32)

    a12=intmin(int32)

    a13=intmax(int64)

    a14=intmin(int64)

    4/11

    下面是long形式,这是matlab的默认显示形式,使matlab以long形式显示的调用方式有

    format long

    format(long)

    2种。matlab帮助文档对short的解释为:Scaled fixed-point format with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. 简单理解就是double型浮点数据保留15为小数,single型浮点数据保留7位小数。

    执行下面的代码:

    clc

    rng(default)

    format long

    a1=pi

    a1_s=single(pi)

    a2=1/3

    a3=rand(3,4)

    a4=eps

    a5=realmax

    a6=realmin

    a7=intmax(int8)

    a8=intmin(int8)

    a9=intmax(int16)

    a10=intmin(int16)

    a11=intmax(int32)

    a12=intmin(int32)

    a13=intmax(int64)

    a14=intmin(int64)

    5/11

    下面是shortE形式,这是matlab的默认显示形式,使matlab以shortE形式显示的调用方式有

    format shortE

    format(shortE)

    2种。matlab帮助文档对short的解释为:

    Floating-point format, with 4 digits after the decimalpoint. For example, 3.1416e+000.

    Integer-valued floating-pointnumbers with a maximum of 9 digits are not displayed in scientificnotation.

    简单理解就是以指数形式显示,浮点数底数保留4为小数,整数值小数小于等于9位不以指数形式显示。

    执行下面的代码:

    clc

    rng(default)

    format shorte

    a1=pi

    a1_s=single(pi)

    a2=1/3

    a3=rand(3,4)

    a4=eps

    a5=realmax

    a6=realmin

    a7=intmax(int8)

    a8=intmin(int8)

    a9=intmax(int16)

    a10=intmin(int16)

    a11=intmax(int32)

    a12=intmin(int32)

    a13=intmax(int64)

    a14=intmin(int64)

    a8_f=single(a8)

    a14_f=single(a14)

    6/11

    下面是longE形式,这是matlab的默认显示形式,使matlab以longE形式显示的调用方式有

    formatlongE

    format(longE)

    2种。matlab帮助文档对short的解释为:

    Floating-point format, with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+000.

    Integer-valued floating-point numberswith a maximum of 9 digits are not displayed in scientific notation.

    简单理解就是以指数形式显示,double浮点数底数保留15为小数,single型浮点数保留7位小数,整数值小数小于等于9位不以指数形式显示。

    执行下面的代码:

    clc

    rng(default)

    format shorte

    a1=pi

    a1_s=single(pi)

    a2=1/3

    a3=rand(3,4)

    a4=eps

    a5=realmax

    a6=realmin

    a7=intmax(int8)

    a8=intmin(int8)

    a9=intmax(int16)

    a10=intmin(int16)

    a11=intmax(int32)

    a12=intmin(int32)

    a13=intmax(int64)

    a14=intmin(int64)

    a8_f=single(a8)

    a14_f=single(a14)

    7/11

    下面是shortG形式,这是matlab的默认显示形式,使matlab以shortG形式显示的调用方式有

    formatshortG

    format(shortG)

    2种。matlab帮助文档对short的解释为:

    Fixed- or floating-point, whichever is more readable,with 4 digits after the decimal point. For example, 3.1416.

    SeeExample 5, for a comparison between this format and short.

    Integer-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.

    简单理解就是这个是以short和shortE中最优的形式,整数值小数小于等于9位不以指数形式显示。

    执行下面的代码(可以比较short和shortG的执行结果):

    clc

    format short

    x =

    format shorte

    x

    format shortg

    x

    8/11

    下面是longG形式,这是matlab的默认显示形式,使matlab以longG形式显示的调用方式有

    formatlongG

    format(longG)

    2种。简单理解就是这个是以long和longE中最优的形式显示,这里不再赘述。

    9/11

    下面是shortEng形式和longEng是对应short和long的两种工程计数显示,也就是说指数是3的倍数,我们常说的千、兆等。执行下面的代码:

    clc

    format shortEng

    get(0,format)

    A = 5.123456789;

    for k=1:10

    disp(A)

    A = A * 10;

    end

    format longEng

    get(0,format)

    A = 5.123456789;

    for k=1:10

    disp(A)

    A = A * 10;

    end

    可以看出结果的指数是0,3,6,9等

    10/11

    +,bank,hex,rat样式分别表示显示结果的正负号(负数显示-,正数显示“+”,0显示空),保留两位小数(银行的角和分),十六进制显示和有理数(分数)显示。

    clc

    format +

    a1=pi

    a2=-pi

    a3=0

    format bank

    a3=pi

    a4=1/3

    format hex

    a5=intmax(int64)

    a6=intmin(int64)

    a7=pi

    format rat

    a8=1/3

    a10=pi

    11/11

    最后需要注意上面的format函数只是对当前matlab程序的显示进行设置,下次打开matlab还是原来的设置。要是设置永远生效,要在在matlab preference菜单中设置,具体是file--preference...然后如图

    注意事项

    如果觉得写的好或者有用的话,恳求动下手指点击页面左下角的【大拇指】进行点赞,然后点击页面下方的【投票】并【评论】,可以【收藏】以便以后观看,您的举手之劳能给予作者莫大的帮助,也可以【关注】我浏览其他精彩经验,谢谢支持!

    matlab
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_1029725.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-07 16:51:17  耗时:0.029
    0.0292s