two equal variable if condition
Mostra commenti meno recenti
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
Risposte (1)
David Hill
il 18 Feb 2021
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 Commenti
manoj pandey
il 19 Feb 2021
Walter Roberson
il 19 Feb 2021
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;
Categorie
Scopri di più su MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!