How to add every other number in a row?

7 vues (au cours des 30 derniers jours)
AM
AM le 18 Nov 2022
Commenté : AM le 19 Nov 2022
I am quite new to matlab. I've been trying to add every other number in a 1x53. For example, it gives say 1 2 3 4..., how do I make it into 1 2+1 3+2+1 4+2+3+1 which would give 1 3 5 10?
this is my data
t = [21.3222640867819 17.3750802156164 14.5582686903576 12.6001062285850 11.8611536681443 11.5892376766466 11.4590658136099 11.4561559814220 11.8273436783477 12.2346997542733 12.6663456083089 13.1248884765231 13.6133343555447 14.1560488223624 14.7390294714380 15.3675434711385 16.0478264373233 16.8166063298431 17.6586366542217 18.5859382681035 19.6133059849360 20.8048315931557 22.1489156164215 23.6786431312492 25.4373517749143 27.5646079504175 30.0867647466357 33.1284333916116 36.8725732613024 41.7926940720638 48.2689652602893 57.1866880790909 70.2579667937006 73.5212188601740 77.1993926487091 81.3811191237218 86.1760561669322 116.245636739297 179.602580001465 399.168759934324]
Thank you!

Réponse acceptée

Image Analyst
Image Analyst le 18 Nov 2022
Modifié(e) : Image Analyst le 18 Nov 2022
Explain again how you want 5. Because 3 + 2 + 1 = 6, not 5.
And how is 4+3+2+1 adding only every other element? It looks like it's adding every single element to me? Did you really think this question through?
Did you try conv or cumsum?
v = 1:4
v = 1×4
1 2 3 4
c = cumsum(v)
c = 1×4
1 3 6 10
t = [21.3222640867819 17.3750802156164 14.5582686903576 12.6001062285850 11.8611536681443 11.5892376766466 11.4590658136099 11.4561559814220 11.8273436783477 12.2346997542733 12.6663456083089 13.1248884765231 13.6133343555447 14.1560488223624 14.7390294714380 15.3675434711385 16.0478264373233 16.8166063298431 17.6586366542217 18.5859382681035 19.6133059849360 20.8048315931557 22.1489156164215 23.6786431312492 25.4373517749143 27.5646079504175 30.0867647466357 33.1284333916116 36.8725732613024 41.7926940720638 48.2689652602893 57.1866880790909 70.2579667937006 73.5212188601740 77.1993926487091 81.3811191237218 86.1760561669322 116.245636739297 179.602580001465 399.168759934324]
t = 1×40
21.3223 17.3751 14.5583 12.6001 11.8612 11.5892 11.4591 11.4562 11.8273 12.2347 12.6663 13.1249 13.6133 14.1560 14.7390 15.3675 16.0478 16.8166 17.6586 18.5859 19.6133 20.8048 22.1489 23.6786 25.4374 27.5646 30.0868 33.1284 36.8726 41.7927
c = cumsum(t, 2)
c = 1×40
21.3223 38.6973 53.2556 65.8557 77.7169 89.3061 100.7652 112.2213 124.0487 136.2834 148.9497 162.0746 175.6879 189.8440 204.5830 219.9506 235.9984 252.8150 270.4736 289.0596 308.6729 329.4777 351.6266 375.3053 400.7426 428.3072 458.3940 491.5224 528.3950 570.1877
  1 commentaire
AM
AM le 19 Nov 2022
My bad! I didn't think this thoroughly as it's about midnight when I posted that but I'm glad you got the answer. 'cumsum' works. Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by