sum even and odd places values
조회 수: 9 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
Andrei Bobrov
2016년 11월 7일
편집: Andrei Bobrov
2016년 11월 7일
B = sum(A(2:2:end));
C = sum(A(1:2:end));
댓글 수: 0
Alexandra Harkai
2016년 11월 7일
len = length(A);
B = sum(A(mod(1:len, 2)==0));
C = sum(A(mod(1:len, 2)==1));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!