MATLAB Matrix

After reading the MATLAB Matrix topic, you will understand how to create and manipulate Matrix in MATLAB.

A MATLAB-vector is a one-dimensional array whereas matrix is a two-dimensional array of numbers. Square brackets are used for defining Vector and Matrix in MATLAB. The semi-colon is used within the square brackets for separating the rows. Space or comma used within the square brackets for separating the elements.

MATLABSelecting a single Element of a Matrix

The general form is:

c = x(i,j)

The variable c stores the element at position ith row and jth column of vector or matrix x.

Example 

Aim (1): To extract the element present at 2nd row and 1st column of matrix x as shown below

Program (1):

x = [2,3,4;1,2,34;11,22,324]

c = x(2,1)

Output (1)

c =
     1

MATLABSelecting the complete row of Matrix

In some cases, for analysis purpose, we need to select either a complete row or column of the matrix. Selecting the complete row of a Vector or Matrix. The general form is:

c = x(i,:)

The variable c stores complete ith row of vector or matrix x.The colon used for performing this operation and here the meaning of the colon is “to select all”.

Example

Aim (1): To extract the 2nd row of matrix x as shown below

Program (1):

x = [2,3,4;1,2,34;11,22,324]

c = x(2,:)

Output (1)

x =
     2     3     4
     1     2    34
    11    22   324

c =
     1     2    34

MATLABSelecting the complete column of Matrix

The general form is:

c = x(:,j)

The variable c stores the complete jth column of a vector or matrix x.The colon used for performing this operation and here the meaning of the colon is “to select all”.

Example 

Program (1): To extract the 1st column of matrix x as shown below

x = [2,3,4;1,2,34;11,22,324]

c = x(:,1)

Output (1)

x =
     2     3     4
     1     2    34
    11    22   324

c =
     2 
     1   
     11

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *