Issue with fprint() on a (2*3) matrix

조회 수: 1 (최근 30일)
Isma
Isma 2015년 7월 2일
댓글: Isma 2015년 7월 2일
Hi,
I am currently looking for advice to understand the cause the following fprint() matter, to the extent that matrix A is set up correctly:
A=[norm_1d WHS_1d STABLE_1d;normES_1d WHS_ES1d STABLE_ES1d];
fprintf('norm\t whs\t stbl\n');
fprintf('%12.8f %12.8f %12.8f\n',A);
output =[0.0203 0.0233 0.0242 ; 0.0340 0.0301 0.0702]
whereas
expected_output=[0.0203 0.0242 0.0301 ; 0.0233 0.0340 0.0702]
2/ To increase readability on my screen is there a way of adding a descriptive empty column with 2 strings 'va' && 'es' such as:
norm whs stbl
va 0.0203 0.0242 0.0301
es 0.0233 0.0340 0.0702
Thanks and regards

채택된 답변

James Tursa
James Tursa 2015년 7월 2일
1) A is being printed in the order the elements are in memory. Since the elements are being stored by columns, the order in memory is A(1,1), A(2,1), A(1,2), A(2,2), A(1,3), A(2,3) and that is the order the elements are printed. To get the printing to be by rows instead, just print A' instead of A.
2) If this is fixed text, you could just add this to the format.
fprintf('va %12.8f %12.8f %12.8f\nes %12.8f %12.8f %12.8f\n',A');
  댓글 수: 1
Isma
Isma 2015년 7월 2일
speechless. Just excellent && amazing ( both 1/ && 2/ are tackled quickly with a concise explanation)! respect.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brendan Hamm
Brendan Hamm 2015년 7월 2일
MATLAB is a column major language, so when it reads in elements of A it reads in order A(1,1), A(2,1),...A(end,1),A(2,1),A(2,2),...A(end,2),...
So to get the output you are looking for transpose A:
fprintf('%12.8f %12.8f %12.8f\n',A.');
  댓글 수: 1
Isma
Isma 2015년 7월 2일
Thanks a lot for the quick and concise feedback, solving point 1/.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by