Create a matrix from two matrices

2 views (last 30 days)
luca
luca on 10 Oct 2019
Answered: Stephen23 on 10 Oct 2019
Given two matrices
S = [1 2 1 2 1 3 4; 5 10 15 20 25 30 35];
G = [1 2 2 3 4 5 ; 7 10 15 20 25 30];
I want to create a new matrix GG where the first raw is given by the first raw of S merged with the one of G. While the second raw is the second raw of S plus the second row of G.
GG = [1 2 1 2 1 3 4 1 2 2 3 4 5; 5 10 15 20 25 30 35 42 45 50 55 60 65]
Note that wth the secnd row of S I arrive at 35, then I add 7 and arrive to 42. Then I don't want to add 10 but 10-7=3, and arrive at 42. I want always to add the difference between the term and previous one. considering that the first one (7) is just 7, cause it is 7-0=7.
May someone help me with this code?

Accepted Answer

Stephen23
Stephen23 on 10 Oct 2019
>> GG = [S,[G(1,:);S(2,end)+cumsum(diff([0,G(2,:)]))]]
GG =
1 2 1 2 1 3 4 1 2 2 3 4 5
5 10 15 20 25 30 35 42 45 50 55 60 65

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!