Accurately printing two vectors with fprintf function
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Matt Graham
le 15 Fév 2015
Commenté : Rahul Pillai
le 4 Oct 2017
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 commentaires
Réponse acceptée
Star Strider
le 15 Fév 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 commentaires
Rahul Pillai
le 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!