How to sum over each n-th element in an array ?

2 visualizaciones (últimos 30 días)
Agnivo Gosai
Agnivo Gosai el 30 de Sept. de 2019
Editada: Stephen23 el 30 de Sept. de 2019
I have a 1 x 108108 array (named output) and I am trying to calculate the sum of elements in this fashion : a1 = sum (1st element + 1002th element + 2003th element +....+ (108108-1001)th element), for each 1008th element after the beginning one. The next sum would be a2 = sum (2nd element + 1003th element + 2004th element....+(108108-1001)th element) and so on till, a1001 = sum (1001th element + 2002th element + 3003th element +....108108th element). Afterwards, I want to collect a1,a2,a3...,a1001 into an array.
Could somebody point me to some documentation that will help me to do this ?
Thanks

Respuesta aceptada

Stephen23
Stephen23 el 30 de Sept. de 2019
Editada: Stephen23 el 30 de Sept. de 2019
Just use reshape:
>> b = sum(reshape(a,1001,[]),2);
and checking the output:
>> b(1)
ans = -6343.8
>> sum(a(1:1001:end))
ans = -6343.8
>> b(2)
ans = -6345.5
>> sum(a(2:1001:end))
ans = -6345.5

Más respuestas (1)

Categorías

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

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by