How can I use fprintf to print an mxn matrix of values to a file

23 ビュー (過去 30 日間)
Thomas Burbey
Thomas Burbey 2019 年 9 月 19 日
コメント済み: Star Strider 2020 年 11 月 6 日
I have a calculated flow net of values in a matrix that I want to print in order (mxn)
How can I print the matrix to a file that has the matrix (rows x cols) in order. How would I do this using fprintf since I don't see any way to repeat using this function.

採用された回答

Star Strider
Star Strider 2019 年 9 月 19 日
There are a numberr of ways to write your matrix, depending on what you want to do.
Since you want to use fprintf, if ‘M’ is your matrix:
fid = fopen( ... , 'wt')
fprintf(fid, [repmat('%10.5f\t', 1, size(M,2)) '\n'], M') % Note Transpose Operation
fclose(fid);
That should work. Experiment to get the result you want.
  6 件のコメント
tbaracu
tbaracu 2020 年 8 月 16 日
編集済み: tbaracu 2020 年 8 月 16 日
How I can print a group like this:
i x[i] DivDiff
--------------------------------
0 1 5 2 1
1 2 7 5
2 4 17
In which: i = 0, 1, 2 ; x = 1 , 2, 4 and DivDiff is matrix like this (empty spaces could be considered 0 or NaN):
5 2 1
7 5
17
Star Strider
Star Strider 2020 年 11 月 6 日
tbaracu — Post this as a new Question.

サインインしてコメントする。

その他の回答 (1 件)

Minkang Cheng
Minkang Cheng 2020 年 11 月 6 日
Hello
I did [U,S,V]=svd(A)
The S is diagonal matrix of singular values with size oof mxn, then, I use fprintf to print S in a file as
fprintf(fid, [repmat('%10.5f\t', 1, size(S,2)) '\n'], S]
This statement works for U with size of mxm, and V with size of nxn
the result is mxn for S, but the singular values are not in diagonal. Somthing wrong for fprinf or svd function?
Thanks
MK

カテゴリ

Help Center および File ExchangeText Analytics Toolbox についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by