0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.