Create a matrix from two matrices

1 回表示 (過去 30 日間)
luca
luca 2019 年 10 月 10 日
回答済み: Stephen23 2019 年 10 月 10 日
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 Center および File ExchangeResizing 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