Basic Vector Manipulation

조회 수: 2 (최근 30일)
Shani Gal
Shani Gal 2012년 5월 21일
Hi
I have 2 vectors
A=[8,4,5,2,4,6,4,8];
K=[1,3,5,1]
I want to create a third vector like this:
C(1)=sum(A(1))
C(2)=sum(A(1:3))
C(3)=sum(A(1:5))
C(4)=sum(A(1))
Without using a for loop ( this is an example, the vectors I have are larger )
Thanks
Shani

답변 (1개)

Daniel Shub
Daniel Shub 2012년 5월 21일
There might be faster ways depending on K.
A = [8,4,5,2,4,6,4,8];
K = [1,3,5,1]
x = cumsum(A);
x(k)
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2012년 5월 21일
One simple mod:
x = cumsum(A(1:max(K)));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by