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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Manage Products 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by