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

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 18 Nov 2013
Modificato: Azzi Abdelmalek il 18 Nov 2013
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

Più risposte (1)

Image Analyst
Image Analyst il 18 Nov 2013
Did your searches turn up cumsum()?

Categorie

Scopri di più su Manage Products in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by