how to refer and modify matrix elements in Matlab | applied electronics engineering

Latest

how to refer and modify matrix elements in Matlab

By Applied Electronics - Thursday, February 23, 2017 No Comments
In the last tutorial we showed you how to create vectors and matrices in Matlab. In this matlab tutorial we show how to refer and modify matrix elements in Matlab.

Accessing Matrix Elements in Matlab


Create a matrix mat as follows.

To refer to the element 5 whose position in 2nd row, 3rd column we write,

Another way is to use colon operator to refer to part of the matrix. For example to access the submatrix formed by elements 3,4 in first row and 4,5 in the second row we can use the colon operator as follows.


To access or refer to a single row or column in matlab matrix we use the first argument to specify the row number and use the colon operator to specify that we want all column to access single row and similarly we use the colon operator to specify we want all rows and specify the column number as the second argument to get the specific single column. This is very useful method to remember. It becomes very handy when you want to learn numerical methods using Matlab for complex programs and use complex matrix algorithm in Matlab.

So lets say you want to access 1st row of the mat matrix above, then you would use the colon operator as follows,

Or that you want to access the 2nd column then you would write,


These were some examples to access matrix elements in Matlab. Now we show how you would modify an element of the matrix.

Modifying elements in Matlab

To modify an element of a matrix in matlab we use the above accessing technique and use the assignment operator = to assign new value. For example below, we create a matrix called mat and change the value of 1st row, 2nd column which is 3 to 11.


Similarly an entire row or column can be modified using the accessing properties above and the assignment operator followed by the new values.


So in this way you can refer or access and modify matrix elements in Matlab. See the following matlab books:

No Comment to " how to refer and modify matrix elements in Matlab "