Create a matrix from two matrices

조회 수: 2 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by