How to fill a matrix with a condition?
15 views (last 30 days)
I want to make some plots, and for that I need a matrix of which every element corresponds to one point in the xy plane, so I have:
The way I normally do it is:
z(:,:)=<some function I want to plot which includes rho and phi>
This time I need a condition: for rho<=25 I want different function values than for 25<rho&&rho<35. How can this be done? I tried to use a loop:
z(:,:)=<some function values>
z(:,:)=<some other values>
but that didn't work.
Thanks for the help.
sixwwwwww on 8 Dec 2013
Edited: sixwwwwww on 8 Dec 2013
try doing it like this:
[x, y] = meshgrid(-35:.25:35);
[phi, rho] = cart2pol(x, y);
z = zeros(size(x));
z(rho <= 25) = rho(rho <= 25) - phi(rho <= 25); % Here you can use your function 1
z(rho > 25) = rho(rho > 25) + phi(rho > 25); % Here you can use your function 2
mesh(x, y, z)
I hope it helps. Good luck!