0

First of all, I am really new to MATLAB so I'm not sure how to create multiple concurrent conditions for executing a while loop. I have a1(i)x + a2(i)y ≤ b(i) for i = 1, . . ., 16 and when this statement is true, it will execute, for example x=x+1. I have tried to code for that idea:

while (for i=i:16
      a1(i)*x + a2(i)*y < b(i);
       end)
   x=x+1;
end

But of course, that code is wrong, I just want to make the whole for loop as the condition of the while loop. So how can I fix my code to do that? Thank you!

2
  • 1
    It's rather unclear what you want the code to do. Could you please edit the question to provide a more thorough explanation, preferably with a minimal reproducible example, i.e. include sample input and output. Yes, you can wrap loops in loops, something like while x<3; for ii=1:3; a(ii) = ii;end;x=x+1;end works fine. Commented Nov 24, 2020 at 9:40
  • It looks like you could use while true; for ii = 1:16 (...); if <condition>; break;end;end;x=x+1;end; take a look at the documentation page of break. Commented Nov 24, 2020 at 9:48

1 Answer 1

2

You want to use all or any, depending on your desired output

while any( a1 * x + a2 * y < b )
    x = x + 1;
end

% or

while all( a1 * x + a2 * y < b )
    x = x + 1;
end

Read up on vectorisation to see why you can usually avoid loops in MATLAB

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.