QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 958|回复: 1
打印 上一主题 下一主题

Matlab优秀例题之产生一个信息表

[复制链接]
字体大小: 正常 放大
回帖奖励 1 点体力 回复本帖可获得 1 点体力奖励! 每人限 1 次

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-10-11 18:34 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    产生一个信息表
    产生并打印一个数据表是说明函数fprintf函数就用的好方法。下面的脚本文件产生1到10中的所有整数的平方根,平方,立方,并在一个表中显示数据,并带有合适的表头。
    % Script file: table.m
    %
    % Purpose:
    % To create a table of square roots, squares, and
    % cubes.
    %
    % Record of revisions:
    % Date Programmer Description of change
    % ==== ========== =====================
    % 12/20/98 S. J. Chapman Original code
    %
    % Define variables:
    % cube              -- Cubes
    % ii                -- Indexvariable
    % square            -- Squares
    % square_roots      -- Squareroots
    % out               -- Outputarray
    % Print the title of the table.
    fprintf(' Table of Square Roots, Squares, and Cubes\n\n');
    % Print column headings
    fprintf(' Number Square Root Square Cube\n');
    fprintf(' ====== =========== ====== ====\n');
    % Generate the required data
    ii = 1:10;
    square_root = sqrt(ii);
    square = ii.^2;
    cube = ii.^3;
    % Create the output array
    out = [ii' square_root' square' cube'];
    % Print the data
    for ii = 1:10
        fprintf (' %2d %11.4f %6d %8d\n',out(ii,);
    end
    程序运行后,产生的结果为
    >> table
    Table of Square Roots,Squares, and Cubes
    Number Square Root Square Cube
    ====== =========== ==========
      1      1.0000      1       1
      2      1.4142      4       8
      3      1.7321      9      27
      4      2.0000    16       64
      5      2.2361    25      125
      6      2.4495    36      216
      7      2.6458    49      343
      8      2.8284    64      512
      9      3.0000    81      729
    10      3.1623   100     1000


    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!

    413

    主题

    36

    听众

    1854

    积分

    升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    1. % Script file: table.m
    2. %
    3. % Purpose:
    4. % To create a table of square roots, squares, and
    5. % cubes.
    6. %
    7. % Record of revisions:
    8. % Date Programmer Description of change
    9. % ==== ========== =====================
    10. % 12/20/98 S. J. Chapman Original code
    11. %
    12. % Define variables:
    13. % cube              -- Cubes
    14. % ii                -- Index variable
    15. % square            -- Squares
    16. % square_roots      -- Square roots
    17. % out               -- Output array
    18. % Print the title of the table.
    19. fprintf(' Table of Square Roots, Squares, and Cubes\n\n');
    20. % Print column headings
    21. fprintf(' Number Square Root Square Cube\n');
    22. fprintf(' ====== =========== ====== ====\n');
    23. % Generate the required data
    24. ii = 1:10;
    25. square_root = sqrt(ii);
    26. square = ii.^2;
    27. cube = ii.^3;
    28. % Create the output array
    29. out = [ii' square_root' square' cube'];
    30. % Print the data
    31. for ii = 1:10
    32.     fprintf (' %2d %11.4f %6d %8d\n',out(ii,:));
    33. end
    复制代码

    数学中国版主团队!
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-8-1 07:55 , Processed in 0.297323 second(s), 56 queries .

    回顶部