# How to replace elements in a vector in Matlab and Octave

Write the position of the element in the index in round brackets to replace an element of a vector

**v(n)=value**

Write the range of positions and values to replace a group of vector elements

**v(n1:n2)=[values]**

## Examples

**Example 1**

Create a vector

>> v=['a';'b';'c';'d';'e']

To replace the third element with the value 'z', type

>> v(3)='z'

Now the vector consists of the following elements

v =

a

b

z

d

e

**Example 2**

To replace a group of three vector elements from the second to the third position, type

>> v(2:4)=['x' 'y' 'z']

v =

Now the vector consists of the following elements

v =

a

x

y

z

e

https://how.okpedia.org/en/matlab/how-to-replace-elements-in-a-vector-in-matlab-and-octave

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