Find out if X out of Y elements of an array are true
7 次查看(过去 30 天)
显示 更早的评论
I have an array of 5 elements. The array is updated at any index (1,2,3,4,5) in each simulation time to a value TRUE or FALSE. In the next function, I should check if 4 out of 5 elements including the last updated element of the array are TRUE. Is there a way to check this?
0 个评论
采纳的回答
Jos (10584)
2019-5-31
Let TF be your logical array and X the index of the last updated element then
ConditionIsMet = TF(x) && sum(TF) == 4
will be true if your condition is met.
更多回答(1 个)
Arvind Sathyanarayanan
2019-5-31
编辑:Arvind Sathyanarayanan
2019-5-31
You can do something like this :
X=sum(Array(:) == 1);
if X>=4
disp('4 or more elements are true')
end
3 个评论
Jos (10584)
2019-6-1
Logically, when one element changes at a time, (let's say, the k-th), the condition that 4 out of 5 are ture, is met can only happen when that element has become true. So, logically, that check is not really necessary ...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!