If statement to target certain area of f(i,j)?

1 回表示 (過去 30 日間)
Anna Lin
Anna Lin 2021 年 6 月 11 日
コメント済み: Anna Lin 2021 年 6 月 11 日
Assume I have a function of F(i,j) in grid points,
Am i typing my if statement correctly to target the 2 areas shown above? MATLAB console has not shown any error so far. Thanks in advance.
% Subroutine
function k=X(x,y,dx)
if (x <=5 & y <=2) & (x >= 10 & y >= 10)
constant = 85; % beta
else
constant = 100; % alpha
end
k=constant*dx;
end

採用された回答

KSSV
KSSV 2021 年 6 月 11 日
% Subroutine
function k=X(x,y,dx)
if (x <=5 && y <=2) || (x >= 10 && y >= 10)
constant = 85; % beta
else
constant = 100; % alpha
end
k=constant*dx;
end
  3 件のコメント
Steven Lord
Steven Lord 2021 年 6 月 11 日
% if (x <=5 & y <=2) & (x >= 10 & y >= 10)
Is the if condition satisfied for x = 3 and y = 1?
x = 3;
y = 1;
condition = (x <=5 & y <=2) & (x >= 10 & y >= 10)
condition = logical
0
Why is this false when the point is in the left box in your diagram?
inLeftBox = (x <=5 & y <=2)
inLeftBox = logical
1
inRightBox = (x >= 10 & y >= 10)
inRightBox = logical
0
The first condition is true but the second is false and:
true & false % false
ans = logical
0
In fact this if statement's condition cannot be satisfied. Can you think of an x coordinate that is simultaneously less than or equal to 5 and greater than or equal to 10? Or to put it another way, point out a point in your diagram that's in both the beta boxes.
How about the second if statement?
% if (x <=5 & y <=2) | (x >= 10 & y >= 10)
condition2 = (x <=5 & y <=2) | (x >= 10 & y >= 10)
condition2 = logical
1
true | false
ans = logical
1
Anna Lin
Anna Lin 2021 年 6 月 11 日
Now I get it, thank you so much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by