필터 지우기
필터 지우기

sum, vector elements, except i-th

조회 수: 10 (최근 30일)
mehrdad asadi
mehrdad asadi 2015년 7월 8일
댓글: mehrdad asadi 2015년 7월 8일
hi,
how can I sum vector elements, say v = rand(1,30), except 12th one?
thanks,

채택된 답변

Mona
Mona 2015년 7월 8일
You can always do the sum, then subtract that single element, 12th, from the sum.
v = rand(1,30);
sum_except12=sum(v)-v(12)

추가 답변 (1개)

Guillaume
Guillaume 2015년 7월 8일
편집: Guillaume 2015년 7월 8일
some other options:
sum(v([1:11, 13:end]))
sum(v(setdiff(1:end, 12))
This latter one, will also work if you have a set of indices you want to exclude. e.g.:
sum(v(setdiff(1:end, [5 12 18])) %sum of v except elements 5, 12, and 18
  댓글 수: 1
mehrdad asadi
mehrdad asadi 2015년 7월 8일
Guillaume,
your answers are always different.
thanks and regards,

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by