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

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 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 2 日
give a short example of your desired result
Anthony
Anthony 2018 年 11 月 2 日
編集済み: Anthony 2018 年 11 月 2 日
my desired result is two new arrays that have the averages of y across the given intervals of x.
Edit: Maybe easier to think of x as a length measurement - as in, I want the average value of y for every 0.1m interval
Bruno Luong
Bruno Luong 2018 年 11 月 2 日
Description inaccurate and contradictory.
It looks like the "cumulative sum" is actually y (100 elements).
x never reaches 0.1.
Cumulative sum on x and y never give 0.1 crossing at 100 elements.
Anthony
Anthony 2018 年 11 月 2 日
編集済み: Anthony 2018 年 11 月 2 日
It doesn't have to be 100 elements, just using an example. In my situation, I do not know how many cells of x I need to sum over to reach 0.1.
I should also say that it will not likely be exactly 0.1 ... so having a margin of error, or rounding numbers would be useful.
Bruno Luong
Bruno Luong 2018 年 11 月 2 日
編集済み: Bruno Luong 2018 年 11 月 2 日
"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 2018 年 11 月 2 日
Thanks for trying Bruno, you're a peach!

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2018 年 11 月 2 日
編集済み: Matt J 2018 年 11 月 2 日

0 投票

c=cumsum(x);
G=discretize(c, 0:0.1:c(end));
result = splitapply(@mean, y, G),

1 件のコメント

Anthony
Anthony 2018 年 11 月 2 日
This looks like it working how I want... Thanks a lot!

サインインしてコメントする。

その他の回答 (2 件)

madhan ravi
madhan ravi 2018 年 11 月 2 日
編集済み: madhan ravi 2018 年 11 月 2 日

0 投票

mean(y(cumsum(x)<=0.1))

8 件のコメント

Anthony
Anthony 2018 年 11 月 2 日
but this only gives one answer. From my previous edit: think of x as a length measurement - as in, I want the average value of y for every 0.1m interval
thus, what I want to get is average y between 0-10cm, then average y over the next 10cm interval, etc... then end result would be more than one number
madhan ravi
madhan ravi 2018 年 11 月 2 日
see the edited answer
Anthony
Anthony 2018 年 11 月 2 日
Thanks for trying to help, but this deosn't produce a correct output
madhan ravi
madhan ravi 2018 年 11 月 2 日
did you read Brunos comment?
Guillaume
Guillaume 2018 年 11 月 2 日
In case you haven't noticed you were given another answer that matches your description.
"this deosn't produce a correct output" is totally useless if you don't tell us what the correct output is. Vagues descriptions that don't actually match the example you've given doesn't cut it.
As has been pointed out, your statement "the first 100 columns would sum to 0.1" is incorrect. cumsum(x) reaches 0.1 at column 86.
Anthony
Anthony 2018 年 11 月 2 日
Yes, I was incorrect about the 100 columns, as I stated. Regardless, I was quite clear, and someone else figured it out. Thanks for trying to help and taking the time for snide comments ;)
Bruno Luong
Bruno Luong 2018 年 11 月 2 日
Yes 1 over 4 people figures it out ...by chance.
Anthony
Anthony 2018 年 11 月 2 日
Thanks again Bruno, you're the best!

サインインしてコメントする。

Guillaume
Guillaume 2018 年 11 月 2 日

0 投票

sumx = cumsum(x);
meanybelowthreshold = mean(y(sumx <= 0.1));
meanyabovethreshold = mean(y(sumx > 0.1));

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 11 月 2 日

コメント済み:

2018 年 11 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by