Adding elements to an array

128 visualizaciones (últimos 30 días)
Frandy
Frandy el 21 de Sept. de 2011
Comentada: Gonzalo Mata el 22 de Nov. de 2018
Ok, so how do you place elements of one array into another array, so that they are included in either the beginning of the array or the end of the array?

Respuesta aceptada

topdawgnate
topdawgnate el 21 de Sept. de 2011
Editada: MathWorks Support Team el 8 de Nov. de 2018
There are many ways to join elements of two arrays. For example, let’s say you have two 2-by-2 matrices A and B:
A = [1 2; 3 4];
B = [5 6; 7 8];
Then the following commands concatenate B to the end of A horizontally:
H1 = [A B]
H2 = horzcat(A,B)
H3 = cat(2,A,B)
The first argument in the cat function (2) tells it to add B as additional columns of A.
These commands concatenate B to the end of A vertically:
V1 = [A; B]
V2 = vertcat(A,B)
V3 = cat(1,A,B)
The 1 tells cat to add B as additional rows of A.
For additional concatenation examples, see:
  1 comentario
Gonzalo Mata
Gonzalo Mata el 22 de Nov. de 2018
and wich of them is the faster one?

Iniciar sesión para comentar.

Más respuestas (1)

YU-CHENG HUANG
YU-CHENG HUANG el 29 de Sept. de 2017
just using c = [a , b]
ex: a = [1 2 3] b = [4 5 6] c = [a,b]
--> c = [1 2 3 4 5 6]

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by