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 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