How to add last element of vector to next vector

2 visualizzazioni (ultimi 30 giorni)
Consider a vector A= [1 2 3 4 5] and another vector B=[ 10 11 17 22 24]
Now what i want to do is that last element of A i.e. A(end) which in this case is 5 shall be added to the first element of B i.e. B(1 ) which in this case is 10. The result which is 15 shall be placed in any other vector or in A as A=[ 1 2 3 4 5 15]. Uptill now its easy. Now the difficult part where I am stuck. the difference of successive elements after neglecting first element of B is any vector C= [ 6 5 2].
Now I want to form a vector D (or the same vector A can be modified) in the following form [1 2 3 4 5 15 21 26 28] i.e. last element of A is added with the first element of C and is given a position in A at the end. Again this newly added number is placed at the end of A and is added with C(2) element i.e. 5 and added at the end position of A and so on.
Can any one please help?

Risposte (1)

Ameer Hamza
Ameer Hamza il 14 Ott 2020
Try this
A = [1 2 3 4 5];
B = [ 10 11 17 22 24];
D = [A A(end)+B(1)];
D = [D cumsum(diff(B(2:end)))+D(end)];
  4 Commenti
Muhammad  Ali Khan
Muhammad Ali Khan il 14 Ott 2020
@NAVNEET Nayan
D=[1 2 3 4 5 15 21 26 28 55 57 61 113 121 125 195 198 206 ]
The first five elements of D are obtained from vector A i.e. A= [1 2 3 4 5]
after meeting some condition in a loop, I attained a vector B=[ 10 11 17 22 24]. Now D is such that D= [ 1 2 3 4 5 15] where 15 i.e.e last element of D is attained by adding last element of A with first element of B. (5 +10=15).
Further elements in D are obtained by subtracting each element of B with its succsessive element i.e. [6 5 2] and adding it with the current end elemnt of D.
Hence D now becomes D = [1 2 3 4 5 15 21 26 28].
Now again the iteration for loop begins for lets say three more times. For each iteration I attain the follwing
B1 =[ 27 29 33], B2 =[ 52 60 64] and B3 = [ 70 73, 81]
Repeating the above procedure for each B, i.e. adding its first element to the last of element of D placing the result as last emlement of D. the difference of successive elements of B's are added to tht last element of D.
In this case D becomes
D=[1 2 3 4 5 15 21 26 28 55(28+27) 57(55+(29-27)) 61(57+(33-29)) 113(61+52) 121 (113-(60+-52)) 125 195 198 206 ]
please let me know if I am vague.
Thanks
NAVNEET NAYAN
NAVNEET NAYAN il 14 Ott 2020
Got it now. No you are not vague. I was not knowing the entire scenario.

Accedi per commentare.

Categorie

Scopri di più su Verification, Validation, and Test in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by