Add SINGLE element to array or vector

J
Jasper · Nov 19, 2020 · 1.9K views
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?
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 31, 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] 
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!