Sum through an array until a value is reached, then continue

7 vues (au cours des 30 derniers jours)
Anthony
Anthony le 2 Nov 2018
Commenté : Anthony le 2 Nov 2018
Assume you have 2 arrays x = linspace(0.001,0.005,1000); y = linspace(0,1,1000);
I want to start at the beginning of x, and sum consecutive columns until the sum = 0.1, then take the average of y across that range.... then continue doing this until the end of x
  6 commentaires
Bruno Luong
Bruno Luong le 2 Nov 2018
Modifié(e) : Bruno Luong le 2 Nov 2018
"With the values given, the first 100 columns would sum to 0.1"
No body know where the 100 comes from. Anyone is guessing and people just wastes theirs times for you.
Not give example is better than give one that doesn't make sense.
Anthony
Anthony le 2 Nov 2018
Thanks for trying Bruno, you're a peach!

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 2 Nov 2018
Modifié(e) : Matt J le 2 Nov 2018
c=cumsum(x);
G=discretize(c, 0:0.1:c(end));
result = splitapply(@mean, y, G),
  1 commentaire
Anthony
Anthony le 2 Nov 2018
This looks like it working how I want... Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (2)

madhan ravi
madhan ravi le 2 Nov 2018
Modifié(e) : madhan ravi le 2 Nov 2018
mean(y(cumsum(x)<=0.1))
  8 commentaires
Bruno Luong
Bruno Luong le 2 Nov 2018
Yes 1 over 4 people figures it out ...by chance.
Anthony
Anthony le 2 Nov 2018
Thanks again Bruno, you're the best!

Connectez-vous pour commenter.


Guillaume
Guillaume le 2 Nov 2018
sumx = cumsum(x);
meanybelowthreshold = mean(y(sumx <= 0.1));
meanyabovethreshold = mean(y(sumx > 0.1));

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by