Accurately printing two vectors with fprintf function

조회 수: 20 (최근 30일)
Matt Graham
Matt Graham 2015년 2월 15일
댓글: Rahul Pillai 2017년 10월 4일
I have two vectors (take A and B for example) A = 1:5 B = 10:10:50
and I want to use the fprintf function to print them as column vectors side by side. I tried inputting them as fprintf('%5g %5g\n', A, B)
However, this returns
1 2; 3 4; 5 10; 20 30; 40 50;
ans = 95
How do I use fprintf to display 1 10; 2 20; 3 30; 4 40; 5 50;
, and how do I prevent ans = 95 from showing up? thanks!

채택된 답변

Star Strider
Star Strider 2015년 2월 15일
You can use a for loop, but a much more efficient way is to combine ‘A’ and ‘B’ into a matrix for printing purposes:
A = 1:5;
B = 10:10:50;
AB = [A; B];
fprintf('%5g %5g\n', AB)
produces:
1 10
2 20
3 30
4 40
5 50
and no ‘95’!
  댓글 수: 3
Star Strider
Star Strider 2017년 4월 27일
As always, my pleasure!
Voting for my Answer would be appreciated!
Rahul Pillai
Rahul Pillai 2017년 10월 4일
if true
% code
end
Hey do you know how I can print this more uniformly? For instance when the values of A become 2 digit numbers, the values of B when printing, shift one space to the right. I want it to be uniform:
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
10 100

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by