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!