How to change orientation of matrix elements and dimension of matrix in Matlab | applied electronics engineering

How to change orientation of matrix elements and dimension of matrix in Matlab

By Applied Electronics - Saturday, February 25, 2017 No Comments
Matlab has lots of useful inbuilt functions that you can use to change orientation of matrix elements and dimension of matrix in Matlab. Transpose is one of them but there are also other functions. The other often used and useful are the reshape, flip, fliplf, flipud, rot90 and repmat functions. We will here how to use them and how they work.

Before you read this tutorial you may want to read the previous tutorials on vector and matrices in Matlab:
First lets make a base matrix called mat with dimension 3x4, 3 rows and 4 columns. We will use the randi function to create arbitary matrix elements.
There are thus 12 elements in the matrix and so we could have various arrangment of the elements such that we have different size of matrix like 2x6, 6x2, 4x3, 1x12 or 12x1 etc. To change the mat matrix above to one of the possible matrix we can use the reshape function.

Let's say we want to change or reshape the above 3x4 matrix to a 2x6 matrix. We can do that as follows,

Note that when doing reshaping the original matrix, mat in this case, will not change.

Next we show how to flip the matrix mat left to right or up to down. To flip the matrix left to right we use the fliplr function as follows.

And to flip the matrix up and down we use the flipud function as follows.

Another useful matlab function to reorient matrix elements which is the rot90. By applying rot90 to a matrix like mat, the elements gets rotated by 90 degree counter-clockwise. See th
example below.

Finally we show two functions that changes the matrix dimensions which are the repmat function and the repelem function. They work as follows. The repmat() function replicates a matrix to create another matrix which contains the specified matrix.

Consider a matrix called intmat(internal matrix) with size 2x2 as follows.

Using repmat(intmat, 3,2) we create another matrix with size 3 by 2 such that each element of the new matrix has element intmat.