Question
I have a vector of the format: x = [xval(1) xval(2) … xval(n)] , and I want to add an element to the end, xval(n+1). How do I do that?
Related Questions
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 20, 2026
For an existing vector x, you can assign a new element to the end using direct indexing. For example
x = [1 2 3] x(4) = 4
or
x(end+1) = 4;
where "end" is a special keyword in MATLAB that means the last index in the array. So in your specific case of n elements, it would automatically know that "end" is your "n".
Another way to add an element to a row vector “x” is by using concatenation:
x = [x newval]
or
x = [x, newval]
For a column vector:
x = [x; newval]
Have a different question? Ask here