MATLAB Tutorials class 5: MATLAB inbuild command repmat,repelem,cat,horzcat, and vertcat

MATLAB Tutorials class 5: MATLAB inbuild command repmat,repelem, cat,horzcat, and vertcat


        We discussed MATLAB inbuild command diag,det, and trace in my previous post. We're going to see repmat, repelem, cat,horzcat, and vertcat MATLAB commands and their functions in this post. 

repmat 

repmat command used to repeat the copies of the array. Syntax is

repmat_value=repmat(array,no) 

How to create a matrix in which all elements should be the same in MATLAB?

>> repmat_value=repmat(10,4)

repmat_value =

    10    10    10    10
    10    10    10    10
    10    10    10    10
    10    10    10    10

>> repmat_value=repmat(10,4,3)

repmat_value =

    10    10    10
    10    10    10
    10    10    10
    10    10    10

How to repeat one matrix in square shape and rectangle shape?

>> rand_matrix_creation=randi(2,3)

rand_matrix_creation =

     2     1     1
     1     1     2
     1     2     2

>> repmat_value=repmat(rand_matrix_creation,2,2)

repmat_value =

     2     1     1     2     1     1
     1     1     2     1     1     2
     1     2     2     1     2     2
     2     1     1     2     1     1
     1     1     2     1     1     2
     1     2     2     1     2     2

>> repmat_value=repmat(rand_matrix_creation,2,4)

repmat_value =

     2     1     1     2     1     1     2     1     1     2     1     1
     1     1     2     1     1     2     1     1     2     1     1     2
     1     2     2     1     2     2     1     2     2     1     2     2
     2     1     1     2     1     1     2     1     1     2     1     1
     1     1     2     1     1     2     1     1     2     1     1     2
     1     2     2     1     2     2     1     2     2     1     2     2 

repelem

repmat repeats the whole array but repelem repeats a particular array element. Syntax is

repmat_value=repmat(array,no)

>> rand_matrix_creation=randi(10,1,5)

rand_matrix_creation =

     6     4     2     7     8

In this case, we mentioned 3, so it will repeat each element in 3 times

>> repelem_value=repelem(rand_matrix_creation,3)

repelem_value =

     6     6     6     4     4     4     2     2     2     7     7     7     8     8     8

But in this case, we mentioned 1,3,1,2,1 so it will repeat 6 as 1 one time,4 as 3 times respectively.

>> repelem_value=repelem(rand_matrix_creation,[1,3,1,2,1])

repelem_value =

     6     4     4     4     2     7     7     8

Concatenate (cat)

cat command is used to concatenate array or matrix in MATLAB. Syntax is 

con_array=cat(dim,array1,array2,....arrayN)

How to concatenate array in MATLAB?

>> rand_matrix_creation1=randi(10,3,2)

rand_matrix_creation1 =

     6     6
     5    10
     9     7

>> rand_matrix_creation2=randi(10,3,2)

rand_matrix_creation2 =

    10     3
     3     7
     7     7

If dim=1, it concatenates row-wise(vertically), and if dim=2, it concatenates column-wise(horizontally)
>> con_array=cat(1,rand_matrix_creation1,rand_matrix_creation2)

con_array =

     6     6
     5    10
     9     7
    10     3
     3     7
     7     7

>> con_array=cat(2,rand_matrix_creation1,rand_matrix_creation2)

con_array =

     6     6    10     3
     5    10     3     7
     9     7     7     7

horzcat and vertcat

        horzcat and vertcat command is used to concatenate arrays horizontally and vertically.


>> rand_matrix_creation1=randi(10,3,2)

rand_matrix_creation1 =

     1     7
     3     9
     3     4

>> rand_matrix_creation2=randi(10,3,2)

rand_matrix_creation2 =

     8     7
     7     4
     1    10

>> con_array=horzcat(rand_matrix_creation1,rand_matrix_creation2)

con_array =

     1     7     8     7
     3     9     7     4
     3     4     1    10

>> con_array=vertcat(rand_matrix_creation1,rand_matrix_creation2)

con_array =

     1     7
     3     9
     3     4
     8     7
     7     4
     1    10


and also cat function is used to convert 2 D images to 3 D images. Syntax is 

three_d_im=cat(3,im,im,im) 

we will see this topic detailly in an upcoming post.
 

Reference:


We have discussed the repmat,repelem, cat, horzcat, and vertcat inbuild command in Matlab so far. We will see the next topic in my next post.

Thank you so much😇
Please subscribe😊.
Share with your friends and please provide your feedback about this post🙏🙏.



Comments

  1. Hi Saranya, we saw your freelancer account. Hence came to you for an opportunity we have
    Hi, We need traditional hair styles making and tips for hair care tutorial video to be done on natural long hair
    we need 2-3  videos in each below category
    1. Daily routine hair style
    2. College goer style
    3. Party wear styles
    4. Hair care tips, hair packs etc
    5. Bridal
     
    each video you wil be paid 750 to 2000 based on complexity
    you will get advance before start of first video
    Total budget is around 30000
    requirement is tamil traditional stuff
     our tamil tradition has great value
     hence this requirement
     But all these has to be done be a beautiful long hair person ma
    Can you take this up?
    Regards,Shalini

    ReplyDelete
  2. you can mail me to keshavashalini@gmail.com

    ReplyDelete

Post a Comment

Popular posts from this blog

MATLAB Tutorials class 4 : MATLAB command diag, det, trace

Improved Median filter MATLAB code and explanation ( without inbuilt function and with inbuilt function)