while statements - imposing two conditions

3 次查看(过去 30 天)
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

回答(2 个)

Jonathan Sullivan
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.

Evan
Evan 2013-7-1
编辑:Evan 2013-7-1
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by