fprintf function applied to 3D matrices
1 次查看(过去 30 天)
显示 更早的评论
Hi guys, I'm struggling a bit with fprintf function. I have a 3D matrix A(m,n,k); m,n are properties and k is a time step. I would like to print the matrix in the format A(m,n,1) A(m,n,2) A(m,n,3) and so on till k. I though to create a big 2D matrix and then print it but I was wondering if there is a more elegant solution.
Thank you very much for your help, Gabriele
2 个评论
Walter Roberson
2015-8-5
Are the first two lines to be
A(1,1,1) A(1,1,2) A(1,1,3) ... A(1,1,k)
A(2,1,1) A(2,1,2) A(2,1,3) ... A(2,1,k)
?
采纳的回答
Walter Roberson
2015-8-5
permute() the array so that the dimension to be printed across is down the columns. Create a format by
numcol = 17; %as appropriate
colfmt = '%13.5f'; (or appropriate format)
fmt = [repmat([colfmt ' '], 1, numcol), colfmt, '\n'];
Then
fprintf(fid, fmt, PermutedArray)
5 个评论
Walter Roberson
2015-8-6
numcol = size(A,2) * size(A,3);
fmt = [repmat([colfmt ' '], 1, numcol-1), colfmt, '\n'];
Note: if you don't mind an extra space at the end of the line, then
fmt = [repmat([colfmt ' '], 1, numcol), '\n'];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!