lettura simple

How to replace a row or a column in a matrix Octave

In this lesson I'll explain how to replace a row or a column of a matrix in Octave.

I'll give you a practical example.

Create a 3x3 matrix

>> matrix = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
matrix =
1 2 3
4 5 6
7 8 9

To replace the first row of the matrix, type matrix(1,:)=[-1 -2 -3]

This command replaces the old values [1 2 3] of the first row with new values [-1 -2 -3]

>> matrix(1,:)=[-1 -2 -3]
matrix =
-1 -2 -3
4 5 6
7 8 9

Now replace the second row of the matrix as well, type matrix(2,:)=[-4 -5 -6]

>> matrix(2,:)=[-4 -5 -6]
matrix =
-1 -2 -3
-4 -5 -6
7 8 9

To change the first column of the matrix, type matrix(:,1)=[0 1 2]

This command replaces the previous values of the first column [-1 -4 7] of the matrix with other values [0 1 2]

>> matrix(:,1)=[0 1 2]
matrix =
0 -2 -3
1 -5 -6
2 8 9

You can replace the second column of the matrix in the same way.

Type the command matrix(:,2)=[3 4 0]

>> matrix(:,2)=[3 4 0]
matrix =
0 3 -3
1 4 -6
2 0 9

This way you can change any row or column of the matrix.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin