How to fill a matrix with a condition?

10 Ansichten (letzte 30 Tage)
Juraj
Juraj am 8 Dez. 2013
Kommentiert: sixwwwwww am 8 Dez. 2013
Hello,
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:
[x,y]=meshgrid(-35:.25:35);
[phi,rho]=cart2pol(x,y);
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:
if rho<=25
z(:,:)=<some function values>
else
z(:,:)=<some other values>
but that didn't work.
Thanks for the help.

Akzeptierte Antwort

sixwwwwww
sixwwwwww am 8 Dez. 2013
Bearbeitet: sixwwwwww am 8 Dez. 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!
  2 Kommentare
Juraj
Juraj am 8 Dez. 2013
Thanks a lot mate!
sixwwwwww
sixwwwwww am 8 Dez. 2013
you are wlecome

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by