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



enter image description here



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?










share|improve this question




























    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



    enter image description here



    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?










    share|improve this question


























      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



      enter image description here



      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?










      share|improve this question















      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



      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 21:47









      Adriaan

      12.4k63160




      12.4k63160










      asked Nov 19 at 21:45









      Shika93

      14912




      14912
























          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.






          share|improve this answer























          • 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










          • Interesting explanations and good practice advices, especially the usage of i and j.
            – Bebs
            Nov 20 at 9:40


















          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





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer























          • 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










          • Interesting explanations and good practice advices, especially the usage of i and j.
            – Bebs
            Nov 20 at 9:40















          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.






          share|improve this answer























          • 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










          • Interesting explanations and good practice advices, especially the usage of i and j.
            – Bebs
            Nov 20 at 9:40













          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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










          • Interesting explanations and good practice advices, especially the usage of i and j.
            – 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










          • @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
















          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












          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





          share|improve this answer























          • 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















          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





          share|improve this answer























          • 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













          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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


















          • 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
















          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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft