简体   繁体   中英

How to write a for loop as a condition of a while loop in MATLAB?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM