Check matrix element
up vote
1
down vote
favorite
I need a (rho,theta) meshgrid and to do that first I defined the meshgrid in Cartesian coordinates and then convert it to polar coordinates:
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
PHI = atan2(Y,X);
Now what I get is a mesh in polar coordinates, but since it is a squared mesh, I get this thing

I say that the values greater than R are wrong and therefore I set them to zero. I did it in this way
for i = 1:1:length(R)
for j = 1:1:length(R)
if R(i,j) > a
R(i,j) = 0;
else
R(i,j);
end
end
end
How can I do this less convoluted?
matlab matrix polar-coordinates cartesian-coordinates
add a comment |
up vote
1
down vote
favorite
I need a (rho,theta) meshgrid and to do that first I defined the meshgrid in Cartesian coordinates and then convert it to polar coordinates:
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
PHI = atan2(Y,X);
Now what I get is a mesh in polar coordinates, but since it is a squared mesh, I get this thing

I say that the values greater than R are wrong and therefore I set them to zero. I did it in this way
for i = 1:1:length(R)
for j = 1:1:length(R)
if R(i,j) > a
R(i,j) = 0;
else
R(i,j);
end
end
end
How can I do this less convoluted?
matlab matrix polar-coordinates cartesian-coordinates
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need a (rho,theta) meshgrid and to do that first I defined the meshgrid in Cartesian coordinates and then convert it to polar coordinates:
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
PHI = atan2(Y,X);
Now what I get is a mesh in polar coordinates, but since it is a squared mesh, I get this thing

I say that the values greater than R are wrong and therefore I set them to zero. I did it in this way
for i = 1:1:length(R)
for j = 1:1:length(R)
if R(i,j) > a
R(i,j) = 0;
else
R(i,j);
end
end
end
How can I do this less convoluted?
matlab matrix polar-coordinates cartesian-coordinates
I need a (rho,theta) meshgrid and to do that first I defined the meshgrid in Cartesian coordinates and then convert it to polar coordinates:
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2+Y.^2);
PHI = atan2(Y,X);
Now what I get is a mesh in polar coordinates, but since it is a squared mesh, I get this thing

I say that the values greater than R are wrong and therefore I set them to zero. I did it in this way
for i = 1:1:length(R)
for j = 1:1:length(R)
if R(i,j) > a
R(i,j) = 0;
else
R(i,j);
end
end
end
How can I do this less convoluted?
matlab matrix polar-coordinates cartesian-coordinates
matlab matrix polar-coordinates cartesian-coordinates
edited Nov 19 at 21:47
Adriaan
12.4k63160
12.4k63160
asked Nov 19 at 21:45
Shika93
14912
14912
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
For direct Cartesian to polar conversion of coordinates: use cart2pol.
[theta,rho] = cart2pol(x,y)
Then a simple logical check will do:
tmp = rho>R;
rho(tmp)=0; %= to delete completely
theta(tmp)=0;
For what it's worth: you can of course create a direct polar grid:
[theta,rho] = meshgrid(0:dtheta:theta,0:dR:R)
Finally: i and j denote the imaginary unit in MATLAB, and I'd argue against using them as regular variables for reasons mentioned in this post, but that's my opinion.
I thought to create a polar grid directly but I was having issues to plot the results usingquiverdue to the different dimension of the matrixthetaandrho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage
– Shika93
Nov 19 at 22:26
@Shika93 you could just make yourthetaandrhovectors which you feed tomeshgridthe same length, and thenquiveris fine with it. Anyway thecart2poldoes basically the same as what you did by hand, just wrapped in a handy function.
– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage ofiandj.
– Bebs
Nov 20 at 9:40
add a comment |
up vote
1
down vote
If we say that a is the limit you want to use you can use below code instead of the for loop:
R = (R<=a).*R
Or you can use as well:
R(R>a) = 0
Just notice that in your code you usedRto denote radius andato denote the limit. Whereas in the figure you useRas the limit andrhofor radius. I used the notation you stated in your code.
– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
For direct Cartesian to polar conversion of coordinates: use cart2pol.
[theta,rho] = cart2pol(x,y)
Then a simple logical check will do:
tmp = rho>R;
rho(tmp)=0; %= to delete completely
theta(tmp)=0;
For what it's worth: you can of course create a direct polar grid:
[theta,rho] = meshgrid(0:dtheta:theta,0:dR:R)
Finally: i and j denote the imaginary unit in MATLAB, and I'd argue against using them as regular variables for reasons mentioned in this post, but that's my opinion.
I thought to create a polar grid directly but I was having issues to plot the results usingquiverdue to the different dimension of the matrixthetaandrho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage
– Shika93
Nov 19 at 22:26
@Shika93 you could just make yourthetaandrhovectors which you feed tomeshgridthe same length, and thenquiveris fine with it. Anyway thecart2poldoes basically the same as what you did by hand, just wrapped in a handy function.
– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage ofiandj.
– Bebs
Nov 20 at 9:40
add a comment |
up vote
3
down vote
accepted
For direct Cartesian to polar conversion of coordinates: use cart2pol.
[theta,rho] = cart2pol(x,y)
Then a simple logical check will do:
tmp = rho>R;
rho(tmp)=0; %= to delete completely
theta(tmp)=0;
For what it's worth: you can of course create a direct polar grid:
[theta,rho] = meshgrid(0:dtheta:theta,0:dR:R)
Finally: i and j denote the imaginary unit in MATLAB, and I'd argue against using them as regular variables for reasons mentioned in this post, but that's my opinion.
I thought to create a polar grid directly but I was having issues to plot the results usingquiverdue to the different dimension of the matrixthetaandrho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage
– Shika93
Nov 19 at 22:26
@Shika93 you could just make yourthetaandrhovectors which you feed tomeshgridthe same length, and thenquiveris fine with it. Anyway thecart2poldoes basically the same as what you did by hand, just wrapped in a handy function.
– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage ofiandj.
– Bebs
Nov 20 at 9:40
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
For direct Cartesian to polar conversion of coordinates: use cart2pol.
[theta,rho] = cart2pol(x,y)
Then a simple logical check will do:
tmp = rho>R;
rho(tmp)=0; %= to delete completely
theta(tmp)=0;
For what it's worth: you can of course create a direct polar grid:
[theta,rho] = meshgrid(0:dtheta:theta,0:dR:R)
Finally: i and j denote the imaginary unit in MATLAB, and I'd argue against using them as regular variables for reasons mentioned in this post, but that's my opinion.
For direct Cartesian to polar conversion of coordinates: use cart2pol.
[theta,rho] = cart2pol(x,y)
Then a simple logical check will do:
tmp = rho>R;
rho(tmp)=0; %= to delete completely
theta(tmp)=0;
For what it's worth: you can of course create a direct polar grid:
[theta,rho] = meshgrid(0:dtheta:theta,0:dR:R)
Finally: i and j denote the imaginary unit in MATLAB, and I'd argue against using them as regular variables for reasons mentioned in this post, but that's my opinion.
edited Nov 19 at 21:55
answered Nov 19 at 21:47
Adriaan
12.4k63160
12.4k63160
I thought to create a polar grid directly but I was having issues to plot the results usingquiverdue to the different dimension of the matrixthetaandrho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage
– Shika93
Nov 19 at 22:26
@Shika93 you could just make yourthetaandrhovectors which you feed tomeshgridthe same length, and thenquiveris fine with it. Anyway thecart2poldoes basically the same as what you did by hand, just wrapped in a handy function.
– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage ofiandj.
– Bebs
Nov 20 at 9:40
add a comment |
I thought to create a polar grid directly but I was having issues to plot the results usingquiverdue to the different dimension of the matrixthetaandrho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage
– Shika93
Nov 19 at 22:26
@Shika93 you could just make yourthetaandrhovectors which you feed tomeshgridthe same length, and thenquiveris fine with it. Anyway thecart2poldoes basically the same as what you did by hand, just wrapped in a handy function.
– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage ofiandj.
– Bebs
Nov 20 at 9:40
I thought to create a polar grid directly but I was having issues to plot the results using
quiver due to the different dimension of the matrix theta and rho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage– Shika93
Nov 19 at 22:26
I thought to create a polar grid directly but I was having issues to plot the results using
quiver due to the different dimension of the matrix theta and rho. With the solution that I choose (cartesian-polar-cartesian again) I have a squared mesh, easier to manage– Shika93
Nov 19 at 22:26
@Shika93 you could just make your
theta and rho vectors which you feed to meshgrid the same length, and then quiver is fine with it. Anyway the cart2pol does basically the same as what you did by hand, just wrapped in a handy function.– Adriaan
Nov 20 at 9:34
@Shika93 you could just make your
theta and rho vectors which you feed to meshgrid the same length, and then quiver is fine with it. Anyway the cart2pol does basically the same as what you did by hand, just wrapped in a handy function.– Adriaan
Nov 20 at 9:34
Interesting explanations and good practice advices, especially the usage of
i and j.– Bebs
Nov 20 at 9:40
Interesting explanations and good practice advices, especially the usage of
i and j.– Bebs
Nov 20 at 9:40
add a comment |
up vote
1
down vote
If we say that a is the limit you want to use you can use below code instead of the for loop:
R = (R<=a).*R
Or you can use as well:
R(R>a) = 0
Just notice that in your code you usedRto denote radius andato denote the limit. Whereas in the figure you useRas the limit andrhofor radius. I used the notation you stated in your code.
– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
add a comment |
up vote
1
down vote
If we say that a is the limit you want to use you can use below code instead of the for loop:
R = (R<=a).*R
Or you can use as well:
R(R>a) = 0
Just notice that in your code you usedRto denote radius andato denote the limit. Whereas in the figure you useRas the limit andrhofor radius. I used the notation you stated in your code.
– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
add a comment |
up vote
1
down vote
up vote
1
down vote
If we say that a is the limit you want to use you can use below code instead of the for loop:
R = (R<=a).*R
Or you can use as well:
R(R>a) = 0
If we say that a is the limit you want to use you can use below code instead of the for loop:
R = (R<=a).*R
Or you can use as well:
R(R>a) = 0
edited Nov 19 at 22:05
answered Nov 19 at 21:59
Cedric Zoppolo
1,16511327
1,16511327
Just notice that in your code you usedRto denote radius andato denote the limit. Whereas in the figure you useRas the limit andrhofor radius. I used the notation you stated in your code.
– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
add a comment |
Just notice that in your code you usedRto denote radius andato denote the limit. Whereas in the figure you useRas the limit andrhofor radius. I used the notation you stated in your code.
– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
Just notice that in your code you used
R to denote radius and a to denote the limit. Whereas in the figure you use R as the limit and rho for radius. I used the notation you stated in your code.– Cedric Zoppolo
Nov 19 at 22:07
Just notice that in your code you used
R to denote radius and a to denote the limit. Whereas in the figure you use R as the limit and rho for radius. I used the notation you stated in your code.– Cedric Zoppolo
Nov 19 at 22:07
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
I like the second option. Oh yes I didn't follow the notation that I used in my code inside the image, sorry. It was just to clarify what I needed. Thanks
– Shika93
Nov 19 at 22:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383087%2fcheck-matrix-element%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown