two equal variable if condition
古いコメントを表示
x=1:10:100;
y=1:10:100;
[x,y]=meshgrid(x,y);
for i=[x,y]
if x (:,i) <= 52 && y (:,i) <= 52
z= 0.00000512*x.^2+0.00000512*y.^2
elseif x,y (:,i)> 77
z= 0.0000126*x.^2 + 0.0000126*y.^2
else
z= -0.00000492*x.^2 - 0.00000492*y.^2
end
mesh(x,y,z)
end
回答 (1 件)
David Hill
2021 年 2 月 18 日
Not sure if this is what you are after or not. Your question was not clear.
[x,y]=meshgrid(1:10:100,1:10:100);
z=-0.00000492*x.^2 - 0.00000492*y.^2;
z(x<=52&y<=52)=0.00000512*x(x<=52&y<=52).^2+0.00000512*y(x<=52&y<=52).^2;
z(x>77&y>77)=0.0000126*x(x>77&y>77).^2 + 0.0000126*y(x>77&y>77).^2;\
mesh(x,y,z);
2 件のコメント
manoj pandey
2021 年 2 月 19 日
Walter Roberson
2021 年 2 月 19 日
As a matter of style, I prefer to store the results of the test into a variable and use the variable as required. For example,
mask = x>77&y>77;
z(mask)=0.0000126*x(mask).^2 + 0.0000126*y(mask).^2;
This makes it more obvious that the same condition is being used in all cases, and reduces the risk of accidentally typing the condition different ways in different places. It would also be more efficient as it would not have to calculate the test multiple times.
There is a more obscure reason as well: in general, the evaluation of the test could have "side effects", and storing the results of the test into a variable ensures that a consistent version is used. For example, consider
z(x>77&y>randi(100))=0.0000126*x(x>77&y>randi(100)).^2 + 0.0000126*y(x>77&y>randi(100)).^2;
compared to
mask = x>77&y>randi(100);
z(mask)=0.0000126*x(mask).^2 + 0.0000126*y(mask).^2;
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!