Find out if X out of Y elements of an array are true

조회 수: 7 (최근 30일)
A VP
A VP 2019년 5월 31일
댓글: A VP 2019년 6월 3일
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?

채택된 답변

Jos (10584)
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
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
If they are 1s and 0s, you can just count the non zero elements using the nnz() function.
  댓글 수: 3
Jos (10584)
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 ...
A VP
A VP 2019년 6월 3일
Ok. This is a new learning to me. Thank you.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by