# How to change a row of the matrix in Matlab and Octave

To replace a single row of an array with another one in Matlab/Octave type

**M(n,:)=[ k ]**

- M is the name of the matrix (array)
- n is the number of the row to be replaced
- k is the list of new elements of the row

**Example**

Define a matrix 2x3

>> M = [1 2 3 ; 4 5 6]

M =

1 2 3

4 5 6

To replace the first row of the matrix type M(1,:)=[ 7 8 9]

>> M(1,:)=[ 7 8 9]

This command replaces the values from the first row

M =

7 8 9

4 5 6

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

>> M(2,:)=[ -1 -2 -3]

This command replaces the values on the second row

M =

7 8 9

-1 -2 -3

https://how.okpedia.org/en/matlab/how-to-change-a-row-of-the-matrix-in-matlab-and-octave

**Report us an error or send a suggestion to improve this page**