user129412
user129412

Reputation: 775

While loop with condition consisting of variable amount of if statements in MATLAB

I'm trying to do the following. Say I have a vector, E, of length N. What I want to do is create a while loop of the form

while E(1) < A || E(2) < A || ..... || E(N) < A
do stuff
end

where A is some input value, say 0.5.

However, I'd like this to work for any N (probably up to like, 50), so I can't just type out every single condition. I know that this is probably computationally expensive, but in principle this shouldn't matter for my purposes.

The problem is, I have no idea how to do this. Perhaps I can use some sort of for loop to create a string that is equal to the condition that I want? I'm not familiar enough with this part of MATLAB to know if that is possible, but I'm assuming this is the direction I should be thinking of.

Upvotes: 1

Views: 41

Answers (1)

Bentoy13
Bentoy13

Reputation: 4966

You can compact the test as:

while any(E<A)

Basically, it compute the mask of all values of E less than A, and the function any returns true if at least one element is true.

Upvotes: 4

Related Questions