sum values in a vector to extract nth term into new matrix?

2 ビュー (過去 30 日間)
MacKenzie
MacKenzie 2013 年 11 月 18 日
編集済み: Azzi Abdelmalek 2013 年 11 月 18 日
Hello,
I have a matrix:
A =
3.0000 2.2200
5.0000 2.2187
7.0000 2.2171
9.0000 2.2132
11.0000 2.2168
13.0000 2.2277
15.0000 2.2283
19.0000 2.2451
I want to take first value and put in new matrix:
[ExtractedA] = A(1,:);
Then, I want it to find the next value that is >= 20; i.e. First values in ExtractedA are:
3.0000 2.2200
9.0000 2.2132 %this value because +5+7+9 = 21 (which is >=20)
Then, I need it to start again looking for the next +20 after the 9:
13.0000 2.2277 %this would be next, since 11+13 >= 20.
Any help on the best function to do this? I know this is likely trivial, but my searching is not yielding much!
Thanks!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 18 日
編集済み: Azzi Abdelmalek 2013 年 11 月 18 日
A =[ 3.0000 2.2200
5.0000 2.2187
7.0000 2.2171
9.0000 2.2132
11.0000 2.2168
13.0000 2.2277
15.0000 2.2283
19.0000 2.2451];
a=A(1,:);
b=A(2:end,1);
k=1;
jj=1;
idx=[];
while k<=numel(b)
f(jj)=b(k);
if sum(f(1:jj))>=20
idx(end+1)=k;
jj=1;
else
jj=jj+1;
end
k=k+1;
end
out=[a;A(idx+1,:)]
I'am not sure if this is efficient for big matrices

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 11 月 18 日
Did your searches turn up cumsum()?

カテゴリ

Help Center および File ExchangeManage Products についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by