Create a matrix from two matrices

1 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2019-10-10
>> 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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by