adding each element of a matrix by previous elements of that row.

2 visualizaciones (últimos 30 días)
Hey guys,
I have a matrix and I want to make a new matrix in such a way in each row, each element is the summ of the prevous elements + current element divided by the number of elements. for example if
a = [1 2 3;4 5 6;7 8 9]
a = 3×3
1 2 3 4 5 6 7 8 9
is the first matrix, the second matrix should be;
b = [1 3/2 6/3; 4 9/2 15/3; 7 15/2 24/3]
b = 3×3
1.0000 1.5000 2.0000 4.0000 4.5000 5.0000 7.0000 7.5000 8.0000

Respuesta aceptada

Star Strider
Star Strider el 9 de Sept. de 2021
Try this —
a = [1 2 3;4 5 6;7 8 9]
a = 3×3
1 2 3 4 5 6 7 8 9
acs = cumsum(a,2)
acs = 3×3
1 3 6 4 9 15 7 15 24
b = acs ./ (1:size(a,2))
b = 3×3
1.0000 1.5000 2.0000 4.0000 4.5000 5.0000 7.0000 7.5000 8.0000
ratsb = rats(b)
ratsb = 3×42 char array
' 1 3/2 2 ' ' 4 9/2 5 ' ' 7 15/2 8 '
Experiment to get different results.
.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by