How to add last element of vector to next vector
조회 수: 2 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
Ameer Hamza
2020년 10월 14일
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
NAVNEET NAYAN
2020년 10월 14일
Got it now. No you are not vague. I was not knowing the entire scenario.
참고 항목
카테고리
Help Center 및 File Exchange에서 Verification, Validation, and Test에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!