# Add element to a given vector

2 views (last 30 days)
steve Brian on 10 Apr 2020
Commented: steve Brian on 10 Apr 2020
I have a vector of the format:
x = [X1 X2 … Xn]
I want to add an element to the end, X(n+1). How do I do that?
x = [ X1 X2 .. Xn X(n+1) ]
ex: x = [ 1 4 2 8]
newX = [ 1 4 2 8 9]

per isakson on 10 Apr 2020
Edited: per isakson on 10 Apr 2020
One way
>> x = 1:6
x =
1 2 3 4 5 6
>> x(end+1)=17
x =
1 2 3 4 5 6 17
>>
steve Brian on 10 Apr 2020
Sorry I am not precise .. I want to be an algorithm that takes each element of the vector and when it has reached the end, to add them ( without specifying it) the last element + 1
Something like:
V = [ x1 x2 x3 ...... xN]
Let's say p is the algorithm or function. P will check each element in the vector, and when it reaches its tail, the end of the vector, add +1 to the last element
new V = [ x1 x2 x3 .... xN x(N+1) ]
p = x1 (first element)
p = x1 x2 (second elem)
p = x1 x2 x3 ( third el)
p = x1 x2 x3 .. xN ( oh, the N element is the tail, i need to put the last element to this tail, N +1, to be like N N +1)
finally: new V = [ x1 x2 x3 .... xN x(N+1) ]

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by