sum, vector elements, except i-th

12 visualizaciones (últimos 30 días)
mehrdad asadi
mehrdad asadi el 8 de Jul. de 2015
Comentada: mehrdad asadi el 8 de Jul. de 2015
hi,
how can I sum vector elements, say v = rand(1,30), except 12th one?
thanks,

Respuesta aceptada

Mona
Mona el 8 de Jul. de 2015
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)

Más respuestas (1)

Guillaume
Guillaume el 8 de Jul. de 2015
Editada: Guillaume el 8 de Jul. de 2015
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 comentario
mehrdad asadi
mehrdad asadi el 8 de Jul. de 2015
Guillaume,
your answers are always different.
thanks and regards,

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by