While loop if condition

3 ビュー (過去 30 日間)
Braden Kerr
Braden Kerr 2020 年 11 月 6 日
コメント済み: Braden Kerr 2020 年 11 月 6 日
Hello,
I have a while loop that I want to run if two conditions are met. I have the while loop and the first condition written out and its below.
A(1,k) = .35; %guess value for alpha^0
epsi = .1;
sigma = 2;
xad = X(:,k)+A(1,k)*D(:,k);
left = fun(xad);
right = fun(X(:,k))+ A(1,k)*epsi*grad_fun(X(:,k))'*D(:,k);
A_2(1,j) = A(1,k);
while (left > right) || (
A_2(1,j+1) = A_2(1,j)/(sigma);
xad = X(:,k)+A_2(1,j+1)*D(:,k);
left = fun(xad);
right = F(1,k)+ (A_2(1,j+1)*epsi*D(:,k)')*D(:,k);
j= j+1;
end
However, I also want to add a condition for the while loop to run that if left is an imaginary number, to cycle through the loop.
Is this possible? Thank you

採用された回答

Sriram Tadavarty
Sriram Tadavarty 2020 年 11 月 6 日
Hi Braden,
You can add these conditions in the while loop.
(~isreal(left)) % To check if left is a complex number
(imag(left) ~= 0) % If the check is only to ensure if there is an imaginary content, implies value could be complex
(imag(left) ~= 0 && real(left) == 0) % If the check is to ensure, it is only imaginary number
The usage of || or && depends on the condition, you wanted. If both the conditions to be met, then use &&. If either of conditions have to be met use ||.
Hope this helps.
Regards,
Sriram
  1 件のコメント
Braden Kerr
Braden Kerr 2020 年 11 月 6 日
Yes, thank you, the top condiditon worked best for what I was trying to do

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by