How do I merge two arrays in MATLAB?
There are two ways of doing this:
A) Using brackets operator
B) Using concatenating functions
The brackets operator ( [] ) , that is used for creating an array in MATLAB , also serves the purpose of concatenating arrays .
Consider :
A = B =
1 2 5 6
3 4 7 8
CASE I : Horizontal Concatenation
CASE II : Vertical Concatenation
Note :
1. If you are concatenating arrays horizontally then each component arrays must have same number of rows .
2. If you are concatenating arrays vertically then each component arrays must have same number of columns.
There are mainly three functions used for concatenation namely cat , horzcat , vertcat .
Again, consider
A = B =
1 2 5 6
3 4 7 8
A . cat
This function concatenate arrays along specific dimension .
Syntax :
Example :
B. horzcat
This function concatenates arrays horizontally .
Syntax :
C. vertcat
This function concatenates arrays vertically .
A) Using brackets operator
B) Using concatenating functions
A. Concatenation Of Arrays
(Using brackets operator)The brackets operator ( [] ) , that is used for creating an array in MATLAB , also serves the purpose of concatenating arrays .
Consider :
A = B =
1 2 5 6
3 4 7 8
CASE I : Horizontal Concatenation
C = [A B]
C =
1 2 5 6
3 4 7 8
CASE II : Vertical Concatenation
C = [A ; B ]
C =
1 2
3 4
5 6
7 8
Note :
1. If you are concatenating arrays horizontally then each component arrays must have same number of rows .
2. If you are concatenating arrays vertically then each component arrays must have same number of columns.
B. Concatenation Of Arrays
(Using concatenating functions)There are mainly three functions used for concatenation namely cat , horzcat , vertcat .
Again, consider
A = B =
1 2 5 6
3 4 7 8
A . cat
This function concatenate arrays along specific dimension .
Syntax :
C = cat (dim , A1 , A2 , A3 .... An)
If dim = 1 then vertical concatenation.
dim = 2 then horizontal concatenation.
Example :
B. horzcat
This function concatenates arrays horizontally .
Syntax :
C = horzcat( A1 , A2 , A3 ,..... An)
where ,
A1 , A2 , A3 ......An must have same number of rows .
C. vertcat
This function concatenates arrays vertically .
C = vertcat(A1 , A2 , A3 ,...... An )
where ,
A1 , A2 , A3 ,.......An must have same number of columns .
Comments
Post a Comment