在matlab中打印n*m矩陣。

[英]print n*m matrix in matlab


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

在MATLAB中,我用這種方法打印一個非常大的矩陣:

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

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

但這是不對的!我想把它打印成這樣:(\t在它們之間)

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

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

我搜索了一下,發現是3*3,這很好:

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

But I in my case size change...

但是我的尺寸改變了…

2 个解决方案

#1


14  

You can use the very simple

你可以用非常簡單的方法。

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

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

你將在每行的末尾有一個多余的標簽\t:

>> 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')

#2


2  

You may also want to look at dlmwrite

您還可以查看dlmwrite。

You can set delimiters, precision, etc.

可以設置分隔符、精度等。

dlmwrite('myfile.txt', M, 'delimiter', '\t', 'precision', 6)

Where M is your matrix.

M是矩陣。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/22/7208ebb41636c5190d74001b096c43c1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com