Reputation: 1
Here's the basic setup. I am trying to create a while loop that will iterate until a set condition is below a certain tolerance. However, this loop must be generalized for multiple values within the same matrix. An example (simplified from what I'm currently trying to accomplish):
x = [3; 2]
tolerance = [0,0]
iter = 0
while x > tolerance
x = x - 1;
iter = iter + 1;
end
The issue I am facing is that the while loop will exit as soon as 1 of the values in the function is less than the tolerance. What I intend to occur is that the while loop will continue to iterate on both variables until both are below the desired tolerance. I am unable to have two separate loops because the size of the variable I will be iterating upon is not set at 2 values.
Any kind of help would be greatly appreciated.
Upvotes: 0
Views: 86
Reputation: 12693
Matlab has a couple of related functions, any
and all
that help with this kind of thing.
any, which returns true
if any of the elements are truthy, will help you here:
while any(x>tolerance)
...
end
You can also do other tricks like
while sum(x>tolerance) > 0
to achieve the same thing, but I like how semantically clear any
is.
Upvotes: 2