Trying to print 2 different variables using fprintf.

조회 수: 33 (최근 30일)
Shawn
Shawn 2014년 2월 6일
답변: Walter Roberson 2014년 2월 6일
I have 2 matrix, a=[1 2 3;4 5 6;n n n] and b=[11 22 33;44 55 66;nn nn nn] I am trying to print the 1st variable of a and b then the next etc:
fprintf('A is %6.4f and b is %6.4f', a, b);
the way math lab is working gives an output of:
A is 1 and b is 2
A is 3 and b is 11
A is 22 and b is 33
How can i separate it so it shows
A is 1 and b is 11
A is 2 and b is 22
A is 3 and b is 33
A is n and b is nn

채택된 답변

Walter Roberson
Walter Roberson 2014년 2월 6일
fprintf('A is %6.4f and b is %6.4f', [a(:), b(:)].' );
Note: this will not print in the order you indicated because in MATLAB, the second variable of "a" is the 4, not the 2. If you want to print across the rows instead of down the columns, then
fprintf('A is %6.4f and b is %6.4f', permute(cat(3,a,b), [3 2 1]));
and the first version, taking the values in column order, can be done as
fprintf('A is %6.4f and b is %6.4f', permute(cat(3,a,b), [3 1 2]));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by