How to get sum of a range

8 visualizaciones (últimos 30 días)
Crystal Lee
Crystal Lee el 12 de Ag. de 2018
Editada: Jan el 2 de Oct. de 2021
I am trying to get the code below to output the number of elements within v(a vector) that are below 2 and above 4.
sum(sum(v<2 & v>4))
When I define v and run this, it does not work as I would like it to. eg for
v=[1,3,5];
sum(sum(v<2 & v>4))
I am expecting an output of 2, since there is 1 element below 2(1), and 1 element above 4(5).

Respuesta aceptada

Paolo
Paolo el 12 de Ag. de 2018
You want to use the OR operator here and not the AND. Use:
nnz(v<2 | v>4)
  3 comentarios
Paolo
Paolo el 12 de Ag. de 2018
That really goes without saying, I am sure OP understands that. He is probably confused about the meaning of logical operators.
Jan
Jan el 2 de Oct. de 2021
Editada: Jan el 2 de Oct. de 2021
@Mutia Rahmadini: Please do not attach a new question as comment to ananswer of another question. Post this as a new question including the code and a copy of the error message, to increase your chance to get an answer. Finally, remove this comment. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by