How to get sum of a range
4 次查看(过去 30 天)
显示 更早的评论
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).
0 个评论
采纳的回答
Paolo
2018-8-12
You want to use the OR operator here and not the AND. Use:
nnz(v<2 | v>4)
3 个评论
Paolo
2018-8-12
That really goes without saying, I am sure OP understands that. He is probably confused about the meaning of logical operators.
Jan
2021-10-2
编辑:Jan
2021-10-2
@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.
更多回答(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!