How do I merge two arrays in MATLAB?

There are two ways of doing this:
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 .

Cvertcat
This function concatenates arrays vertically .
C = vertcat(A1 , A2 , A3 ,...... An )
where ,
          A1 , A2 , A3 ,.......An must have same number of columns .

Comments

Popular posts from this blog

What are applications of MATLAB?

How can I learn genetic algorithm using MATLAB (to be precise).

What are some good dissertation tips?