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

matlab arrays

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?

Best Answer

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.