while statements - imposing two conditions
2 views (last 30 days)
Show older comments
Hi there I am trying to impose a while loop with two conditions;
while (L2normpercentold>=tol)&&(any(Diffpart1>0.0001)) However I get the following error;
??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> reconstructionofimagetester6 at 48 while (L2normpercentold>=tol)&&(any(Diffpart1>0.0001))
Any way around this
0 Comments
Answers (2)
Jonathan Sullivan
on 1 Jul 2013
Sounds like either L2normpercentold or Diffpart1 are not scalars. How you get around this depends on what type of behavior you want. Do you want all elements to satisfy the condition? Or would you like be ok with only one of the elements satisfying the condition?
The functions all and any would come in handy here.
0 Comments
Evan
on 1 Jul 2013
Edited: Evan
on 1 Jul 2013
Is Lnormpercentold a scalar or a vector? You can receive this error when attempting to use the && operator to compare logical vectors. So, depending on what you want, you need to add a command like "all" or "any" to the first condition or change the && to & (and probably remove that "any" from the second condition).
Note: another source could be Diffpart1 if Diffpart1 is a matrix, as "any" will only operate on one dimension, returning a vector. Telling us the sizes of the arrays you're using in your comparisons will be helpful.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!