Basic Vector Manipulation

1 回表示 (過去 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 Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by