Accurately printing two vectors with fprintf function
15 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
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 件のコメント
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 Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!