Add SINGLE element to array or vector
5.586 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pedro GUillem
am 12 Mai 2016
Kommentiert: Image Analyst
am 27 Mai 2022
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?
1 Kommentar
Image Analyst
am 27 Mai 2022
@Anushalini Thiyagarajan I have no idea what you mean. Please ask your question in a new question (not here) after you read this:
In the meantime, look at input functions such as readmatrix, importdata, dlmread, xlsread, fgetl, etc.
Akzeptierte Antwort
Image Analyst
am 12 Mai 2016
Bearbeitet: Image Analyst
am 18 Okt. 2020
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]
6 Kommentare
Stefano Cardarelli
am 26 Mär. 2020
Bearbeitet: Stefano Cardarelli
am 26 Mär. 2020
also this works for me, is basically direct indexing:
x(end+1) = newval
Weitere Antworten (2)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!