fprintf two column vectors at once

29 visualizaciones (últimos 30 días)
Sam Lansink
Sam Lansink el 2 de Abr. de 2022
Comentada: Voss el 2 de Abr. de 2022
I have two column vectors of 1x10 and I want to find a way to fprintf or display them next to eachother like such...
vector 1:
0
-0.11
-0.22
-0.33
....
1
vector 2:
1.011
1.024
1.035
....
1.11
And I want to have them come out something like
0 1.011
-0.11 1.024
-0.22 1.035
....
1 1.11
Thank You

Respuesta aceptada

Voss
Voss el 2 de Abr. de 2022
v1 = randn(10,1); % column vector, size 10-by-1
v2 = randn(10,1);
% different options:
[v1 v2]
ans = 10×2
-0.3159 -1.7355 -0.3667 -1.3384 -0.0570 0.4079 -0.6566 0.7896 -0.5214 -0.1512 0.0286 -0.0235 -0.3436 0.1230 -0.5274 1.8187 1.0508 0.0375 0.9348 0.8193
disp([v1 v2]);
-0.3159 -1.7355 -0.3667 -1.3384 -0.0570 0.4079 -0.6566 0.7896 -0.5214 -0.1512 0.0286 -0.0235 -0.3436 0.1230 -0.5274 1.8187 1.0508 0.0375 0.9348 0.8193
fprintf('%6.3f %6.3f\n',[v1 v2].') % pick a format you like
-0.316 -1.736 -0.367 -1.338 -0.057 0.408 -0.657 0.790 -0.521 -0.151 0.029 -0.023 -0.344 0.123 -0.527 1.819 1.051 0.038 0.935 0.819
  2 comentarios
Sam Lansink
Sam Lansink el 2 de Abr. de 2022
I seem to still be a little of because when I input it, it puts all the values from the first vector on top and the second on bottom
Outputting like:
if v1= 0 1 2 3 4
and v2= 5 6 7 8 9
0 1
2 3
4 5
6 7
8 9
Desired output:
0 5
1 6
2 7
3 8
4 9
Voss
Voss el 2 de Abr. de 2022
When you send a matrix to fprintf the elements will get printed in the following order: down the first column first, then down the second column, and so on. So you have to make sure the matrix you send to fprintf has the elements in the correct order.
Constructing that matrix is different if you have row vectors vs if you have column vectors.
% Case 1: row vectors
v1 = 0:4;
v2 = 5:9;
% look at the temporary matrix that is
% constructed and sent to fprintf:
[v1; v2] % fprintf will print 0, 5, 1, 6, 2, 7, 3, 8, 4, 9
ans = 2×5
0 1 2 3 4 5 6 7 8 9
% the format says put a line-break (\n) after every two elements,
% so: 0, 5, line-break, 1, 6, line-break, etc.
fprintf('%d %d\n',[v1; v2]) % (using %d now since these are integers)
0 5 1 6 2 7 3 8 4 9
% Case 2: column vectors
v1 = (0:4).';
v2 = (5:9).';
[v1 v2].' % same matrix as above; different syntax because we have column vectors
ans = 2×5
0 1 2 3 4 5 6 7 8 9
% gives the same result as the row vector case because the matrix is
% constructed differently now
fprintf('%d %d\n',[v1 v2].')
0 5 1 6 2 7 3 8 4 9
If you don't know whether the vectors will be column vectors or row vectors, or you want to write one thing that works for both, you can do that too:
v1 = 0:4; % one row vector
v2 = (5:9).'; % one column vector
% (:) makes each vector into a column vector, for the
% purpose of constructing this matrix for fprintf
[v1(:) v2(:)].'
ans = 2×5
0 1 2 3 4 5 6 7 8 9
fprintf('%d %d\n',[v1(:) v2(:)].')
0 5 1 6 2 7 3 8 4 9

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Elementary Math en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by