Create a matrix from two matrices

3 visualizaciones (últimos 30 días)
luca
luca el 10 de Oct. de 2019
Respondida: Stephen23 el 10 de Oct. de 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?

Respuesta aceptada

Stephen23
Stephen23 el 10 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by