数学建模社区-数学中国

标题: Matlab优秀例题之产生一个信息表 [打印本页]

作者: 森之张卫东    时间: 2015-10-11 18:34
标题: Matlab优秀例题之产生一个信息表
产生一个信息表
产生并打印一个数据表是说明函数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



作者: 森之张卫东    时间: 2015-10-11 18:35
  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
复制代码






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5