Accurately printing two vectors with fprintf function
37 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 15 Feb 2015
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 Comments
Rahul Pillai
on 4 Oct 2017
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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!