在Matlab中打印n * m矩阵 [英] print n*m matrix in matlab

查看:1197
本文介绍了在Matlab中打印n * m矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我以这种方式打印了一个非常大的矩阵:

In MATLAB I'm printing a very large matrix this way:

fid = fopen('c:\\OUTPUT.txt','wt');
fprintf(fid,'%f\t',T');
fclose(fid);

但这是不对的!我想这样打印:(它们之间的\t和行末尾的\n)

But this is not right! I want to print it like this:(\t between them and \n at the end of row)

1   2   3
4   5   6
7   8   9
10  11  12

我搜索发现如果是3 * 3,就可以了:

I searched and found If it was 3*3 this was fine:

fprintf(fid,'%f %f %f\n',T');

但是我要改变大小...

But I in my case size change...

推荐答案

您可以使用非常简单的

fprintf([repmat('%f\t', 1, size(A, 2)) '\n'], A');

不过,每行末尾会有一个多余的制表符\t:

You will have one superfluous tab \t at the end of each line, though:

>> A = magic(5)

A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> fprintf([repmat('%f\t', 1, size(A, 2)) '\n'], A')
17.000000   24.000000   1.000000    8.000000    15.000000   % oh, a tab
23.000000   5.000000    7.000000    14.000000   16.000000   % oh, a tab 
4.000000    6.000000    13.000000   20.000000   22.000000   % oh, a tab
10.000000   12.000000   19.000000   21.000000   3.000000    % oh, a tab
11.000000   18.000000   25.000000   2.000000    9.000000    % oh, a tab

要将输出打印到文件中,只需使用

To print the output to a file, just use

fprintf(fid, [repmat('%f\t', 1, size(A, 2)) '\n'], A')

这篇关于在Matlab中打印n * m矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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