combine two Matrix in one column

3 visualizaciones (últimos 30 días)
Basem Nouh
Basem Nouh el 29 de Mayo de 2019
Comentada: Alex Mcaulley el 29 de Mayo de 2019
Hi
i have two matrix A6*3 and B6*3
i want to combine all elements in one column but it must be sorted like the first element is A(1,1) then A(1,2) till A(6,3) then come all the elements from Matrix B
L=[A(1,1);A(1,2);A(1,3);A(2,1).......A(6,3);B(1,1);........B(6,3)];
thanks

Respuesta aceptada

Alex Mcaulley
Alex Mcaulley el 29 de Mayo de 2019
L = [reshape(A',[numel(A),1]);reshape(B',[numel(B),1])]
  2 comentarios
Basem Nouh
Basem Nouh el 29 de Mayo de 2019
can u please explain how u did that ?
or how can we do it with loop ?
Alex Mcaulley
Alex Mcaulley el 29 de Mayo de 2019
You can see it doing each part separately. Try with this example:
A = [1,2;3,4]
A =
1 2
3 4
>> B = [5,6;7,8]
B =
5 6
7 8
And you want a column vector with all the values:
L = [1;2;3;4;5;6;7;8]
If you execute de following reshape statements you obtain a column vector for each matrix:
reshape(A',[numel(A),1]) %A is transposed because Matlab take the values by columns
% and you want to take them by rows
ans =
1
2
3
4
See this for more information about reshape

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by