sum even and odd places values

I have
A=[4 6 2 1 45 98 2 3 65 12 32 32 15 42 15 14].
how to sum even and odd places values of A. i.e.
B(even places sum)=6+1+98+3+12+32+42+14=208,
C(odd places sum)=4+2+45+2+65+32+15+15=180.
how to do it

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2016 年 11 月 7 日
編集済み: Andrei Bobrov 2016 年 11 月 7 日

3 投票

B = sum(A(2:2:end));
C = sum(A(1:2:end));
Alexandra Harkai
Alexandra Harkai 2016 年 11 月 7 日

0 投票

len = length(A);
B = sum(A(mod(1:len, 2)==0));
C = sum(A(mod(1:len, 2)==1));

カテゴリ

タグ

質問済み:

2016 年 11 月 7 日

編集済み:

2016 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by