MATLAB:打印格式化表格 [英] MATLAB: Printing a formatted table

查看:176
本文介绍了MATLAB:打印格式化表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于MATLAB的迭代程序(解决A x = b 的Jacobi迭代方法),这使我可以进行N次迭代.

I'm working on an iteration program on MATLAB (Jacobi iteration method on solving A x = b ), which allows me to make N iterations.

到目前为止,我可以对迭代进行编码,但是我想改善结果的表示方式.

So far, I can code the iteration, but I want to improve the presentation of the results.

我的程序的输出:

jacobi(A, b, 20);  %N = 20 iterations

x的值,按从左到右的迭代次数列(从0开始):

Values of x by columns of iteration(s) (starting at 0) from left to right:

X =

第1到9列

     0    3.4000   -0.0556    1.3848    1.0250    0.9300    1.0558    0.9813    1.0034
     0    4.0000    0.8603    2.1810    2.0329    1.9030    2.0453    1.9826    1.9997
     0    2.5000    4.5444    2.1023    3.3195    2.9782    2.9513    3.0402    2.9831
     0    2.0000    5.3463    3.5175    4.0772    4.0612    3.9463    4.0266    3.9939
     0    6.7778    3.8111    5.4106    4.9648    4.9366    5.0513    4.9777    5.0046

第10到18列

1.0026    0.9979    1.0011    0.9998    1.0000    1.0001    1.0000    1.0000    1.0000
2.0027    1.9977    2.0008    1.9998    1.9999    2.0001    2.0000    2.0000    2.0000
3.0035    3.0013    2.9984    3.0009    2.9998    3.0000    3.0001    3.0000    3.0000
3.9990    4.0022    3.9988    4.0004    4.0000    3.9999    4.0001    4.0000    4.0000
5.0015    4.9979    5.0011    4.9997    5.0000    5.0001    5.0000    5.0000    5.0000

第19至21列

1.0000    1.0000    1.0000
2.0000    2.0000    2.0000
3.0000    3.0000    3.0000
4.0000    4.0000    4.0000
5.0000    5.0000    5.0000

没关系,如果你们从未听说过这种方法,但是我想知道我是否要插入这样的字符串

Nevermind if you guys never heard of such method, but I'm wondering, if I were to insert strings such that it becomes

jacobi(A, b, 20);

从左到右按迭代列(从0开始)的x值:

Values of x by columns of iteration(s) (starting at 0) from left to right:

X =

第1到9列

   iteration
     ----------------------------------------------------------------------------------
      0  |    1   |    2    |    3    |    4    |    5    |    6    |    7    |     8
     ----------------------------------------------------------------------------------
      0    3.4000   -0.0556    1.3848    1.0250    0.9300    1.0558    0.9813    1.0034
      0    4.0000    0.8603    2.1810    2.0329    1.9030    2.0453    1.9826    1.9997
      0    2.5000    4.5444    2.1023    3.3195    2.9782    2.9513    3.0402    2.9831
      0    2.0000    5.3463    3.5175    4.0772    4.0612    3.9463    4.0266    3.9939
      0    6.7778    3.8111    5.4106    4.9648    4.9366    5.0513    4.9777    5.0046

以此类推,我该怎么做呢?

and so on, how exactly do I do that?

我正在考虑在循环的下面插入------ upi |中间和------以及循环中的迭代次数,以使其生成所需的N个数.

I'm thinking of inserting ------ up, i | middle, and ------ below and number of iterations in the loop to let it generate as many as N desired.

到目前为止,我在搜索中找到的最接近的示例是

So far, the closest example I've found on searches, is

arrayfun(@(x)sprintf('Hole %j',x), 1:18)','UniformOutput',false)

但是,我仍然无法弄清楚如何将此想法整合到我的代码中.在MATLAB中可以进行这种输出吗?

But still, I can't figure out how to incorporate this idea into my code. Is this kind of output possible in MATLAB?

有人可以帮助我吗?预先感谢.

Can anyone help me? Thanks in advance.

我正在学习MATLAB的课程,但是仅用了几个月的课程,我所学到的东西就很少了.但是我正在寻找挑战.

I'm taking a course on MATLAB, but with just a couple of months of lessons, there's not much stuffs I have learned yet. But I'm looking for a challenge.

推荐答案

您实际上并不需要在矩阵本身中添加字符串.

You don't really need to add strings to the matrix itself.

显示时,只需对结果进行格式化即可.例如,您可以采用以下解决方案,该解决方案采用 fprintf 和一些字符串格式(无循环!):

It's all a matter of formatting the result when displaying it. For instance, you can go about the following solution that employs fprintf and some string formatting (no loops!):

% # Initialize table properties
col_w = 11;  % # Fixed column width in characters
fr_n = 4;    % # Number of fraction digits

% # Print header
hdr_line = repmat(['+', char('-' * ones(1, col_w))], 1, size(X, 2));
hdr_fmt = ['|%', int2str(col_w - fr_n - 1)', '.0f', char(' ' * ones(1, fr_n + 1))];
fprintf('Iteration:\n%s\n', hdr_line)
fprintf(hdr_fmt, 0:size(X, 2) - 1)
fprintf('\n%s\n', hdr_line)

% # Print values
data_fmt = [repmat(['|%', int2str(col_w - 1), '.', int2str(fr_n), 'f '], 1, size(X, 2)), '\n'];
fprintf(data_fmt, X')

此处X表示要打印的矩阵(不要忘记在最后一行中转置X!).
col_w = 10fr_n = 4的示例输出:

Here X denotes the matrix that you want to print (don't forget to transpose X in the last line!).
Sample output for col_w = 10 and fr_n = 4:

Iteration:
+----------+----------+----------+----------+----------+----------+----------+----------+----------
|    0     |    1     |    2     |    3     |    4     |    5     |    6     |    7     |    8     
+----------+----------+----------+----------+----------+----------+----------+----------+----------
|   0.0000 |   3.4000 |  -0.0556 |   1.3848 |   1.0250 |   0.9300 |   1.0558 |   0.9813 |   1.0034 
|   0.0000 |   4.0000 |   0.8603 |   2.1810 |   2.0329 |   1.9030 |   2.0453 |   1.9826 |   1.9997 
|   0.0000 |   2.5000 |   4.5444 |   2.1023 |   3.3195 |   2.9782 |   2.9513 |   3.0402 |   2.9831 
|   0.0000 |   2.0000 |   5.3463 |   3.5175 |   4.0772 |   4.0612 |   3.9463 |   4.0266 |   3.9939 
|   0.0000 |   6.7778 |   3.8111 |   5.4106 |   4.9648 |   4.9366 |   5.0513 |   4.9777 |   5.0046 

这篇关于MATLAB:打印格式化表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆