Accurately printing two vectors with fprintf function

15 ビュー (過去 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 Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by