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]
A little help, please!

 採用された回答

per isakson
per isakson 2020 年 4 月 10 日
編集済み: per isakson 2020 年 4 月 10 日

0 投票

One way
>> x = 1:6
x =
1 2 3 4 5 6
>> x(end+1)=17
x =
1 2 3 4 5 6 17
>>

5 件のコメント

steve Brian
steve Brian 2020 年 4 月 10 日
I can't do with a for loop to look for the final elements, then add to the last element that n + 1?
Because, I have a vector of n elements, and I want to add them to n + 1 at the end, without specifying the element
per isakson
per isakson 2020 年 4 月 10 日
I'm not sure I understand. What about this
x = [ 1 4 2 8];
newX = [ x, 9 ]
newX =
1 4 2 8 9
steve Brian
steve Brian 2020 年 4 月 10 日
Let's say we have a matrix of MxN, I insert the elements of the matrix into a vector, then I want the last element to be n + 1
V = [ 12 21 10;
7 2 1;
8 5 21; ]
NewV = [ 12 21 10 7 2 1 8 5 21 "22"]
I have already the newV, but i want to add that n+1 element
per isakson
per isakson 2020 年 4 月 10 日
I'm even more confused
"I have already the newV, but i want to add that n+1 element"
Isn't that exactly what's done by
NewV(end+1) = 22;
steve Brian
steve Brian 2020 年 4 月 10 日
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) ]

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by