Borrar filtros
Borrar filtros

how do i join 2 matrices

3 visualizaciones (últimos 30 días)
Johannes Deelstra
Johannes Deelstra el 22 de Nov. de 2022
Comentada: Johannes Deelstra el 22 de Nov. de 2022
I have 2 matrices a and b, both having size 5 x 5
I want to combine these 2 in a new matrix c, such that c = [a(:,1) b(:,1) a(:,2) b(:,2) a(:,3) b(:,3) etc];
Is this the only way doing it or is there another way?
Thanks
Johannes

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 22 de Nov. de 2022
Yes, you could use vertical concatenation followed by reshape.
a= rand(5)
a = 5×5
0.0173 0.7698 0.9666 0.5950 0.4746 0.6177 0.5225 0.8442 0.4543 0.3936 0.6916 0.9324 0.3006 0.4929 0.6506 0.2824 0.6577 0.2550 0.7117 0.3328 0.9762 0.9710 0.0704 0.4868 0.8103
b=rand(5)+5
b = 5×5
5.7310 5.9541 5.6878 5.2669 5.0292 5.6420 5.3310 5.8203 5.4492 5.9954 5.5594 5.1451 5.9291 5.2707 5.2556 5.1165 5.3710 5.5359 5.6458 5.9792 5.6456 5.7392 5.6668 5.4423 5.0321
% brute force
c = [a(:,1) b(:,1) a(:,2) b(:,2) a(:,3) b(:,3)]
c = 5×6
0.0173 5.7310 0.7698 5.9541 0.9666 5.6878 0.6177 5.6420 0.5225 5.3310 0.8442 5.8203 0.6916 5.5594 0.9324 5.1451 0.3006 5.9291 0.2824 5.1165 0.6577 5.3710 0.2550 5.5359 0.9762 5.6456 0.9710 5.7392 0.0704 5.6668
% using vertical concatenation and reshape
c2 = [a;b]
c2 = 10×5
0.0173 0.7698 0.9666 0.5950 0.4746 0.6177 0.5225 0.8442 0.4543 0.3936 0.6916 0.9324 0.3006 0.4929 0.6506 0.2824 0.6577 0.2550 0.7117 0.3328 0.9762 0.9710 0.0704 0.4868 0.8103 5.7310 5.9541 5.6878 5.2669 5.0292 5.6420 5.3310 5.8203 5.4492 5.9954 5.5594 5.1451 5.9291 5.2707 5.2556 5.1165 5.3710 5.5359 5.6458 5.9792 5.6456 5.7392 5.6668 5.4423 5.0321
c2 = reshape(c2,size(a,1),[])
c2 = 5×10
0.0173 5.7310 0.7698 5.9541 0.9666 5.6878 0.5950 5.2669 0.4746 5.0292 0.6177 5.6420 0.5225 5.3310 0.8442 5.8203 0.4543 5.4492 0.3936 5.9954 0.6916 5.5594 0.9324 5.1451 0.3006 5.9291 0.4929 5.2707 0.6506 5.2556 0.2824 5.1165 0.6577 5.3710 0.2550 5.5359 0.7117 5.6458 0.3328 5.9792 0.9762 5.6456 0.9710 5.7392 0.0704 5.6668 0.4868 5.4423 0.8103 5.0321

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by