Set every element to zero in matrix unless it's `1` or `1/2`












1












$begingroup$


I have the following code:



max = 1;

PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];

a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm


That generates this matrix:



<span class=$$ $$">





How do I set every element that is not equal to 1 or 1/2 to 0?










share|improve this question











$endgroup$












  • $begingroup$
    Something like a /. {1 -> 0, 1/2 -> 0} should do it.
    $endgroup$
    – Carl Lange
    1 hour ago










  • $begingroup$
    Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
    $endgroup$
    – SuperCiocia
    1 hour ago










  • $begingroup$
    Also, your command does not change anything to my a
    $endgroup$
    – SuperCiocia
    1 hour ago






  • 1




    $begingroup$
    Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
    $endgroup$
    – Lukas Lang
    1 hour ago
















1












$begingroup$


I have the following code:



max = 1;

PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];

a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm


That generates this matrix:



<span class=$$ $$">





How do I set every element that is not equal to 1 or 1/2 to 0?










share|improve this question











$endgroup$












  • $begingroup$
    Something like a /. {1 -> 0, 1/2 -> 0} should do it.
    $endgroup$
    – Carl Lange
    1 hour ago










  • $begingroup$
    Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
    $endgroup$
    – SuperCiocia
    1 hour ago










  • $begingroup$
    Also, your command does not change anything to my a
    $endgroup$
    – SuperCiocia
    1 hour ago






  • 1




    $begingroup$
    Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
    $endgroup$
    – Lukas Lang
    1 hour ago














1












1








1





$begingroup$


I have the following code:



max = 1;

PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];

a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm


That generates this matrix:



<span class=$$ $$">





How do I set every element that is not equal to 1 or 1/2 to 0?










share|improve this question











$endgroup$




I have the following code:



max = 1;

PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];

a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm


That generates this matrix:



<span class=$$ $$">





How do I set every element that is not equal to 1 or 1/2 to 0?







list-manipulation matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 36 mins ago









gwr

7,81322558




7,81322558










asked 1 hour ago









SuperCiociaSuperCiocia

515311




515311












  • $begingroup$
    Something like a /. {1 -> 0, 1/2 -> 0} should do it.
    $endgroup$
    – Carl Lange
    1 hour ago










  • $begingroup$
    Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
    $endgroup$
    – SuperCiocia
    1 hour ago










  • $begingroup$
    Also, your command does not change anything to my a
    $endgroup$
    – SuperCiocia
    1 hour ago






  • 1




    $begingroup$
    Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
    $endgroup$
    – Lukas Lang
    1 hour ago


















  • $begingroup$
    Something like a /. {1 -> 0, 1/2 -> 0} should do it.
    $endgroup$
    – Carl Lange
    1 hour ago










  • $begingroup$
    Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
    $endgroup$
    – SuperCiocia
    1 hour ago










  • $begingroup$
    Also, your command does not change anything to my a
    $endgroup$
    – SuperCiocia
    1 hour ago






  • 1




    $begingroup$
    Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
    $endgroup$
    – Lukas Lang
    1 hour ago
















$begingroup$
Something like a /. {1 -> 0, 1/2 -> 0} should do it.
$endgroup$
– Carl Lange
1 hour ago




$begingroup$
Something like a /. {1 -> 0, 1/2 -> 0} should do it.
$endgroup$
– Carl Lange
1 hour ago












$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago




$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago












$begingroup$
Also, your command does not change anything to my a
$endgroup$
– SuperCiocia
1 hour ago




$begingroup$
Also, your command does not change anything to my a
$endgroup$
– SuperCiocia
1 hour ago




1




1




$begingroup$
Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
$endgroup$
– Lukas Lang
1 hour ago




$begingroup$
Look up Except. Also, if you want to change a, you have to assign the result to a again. (In Mathematica, barely anything changes variables, most things are immutable)
$endgroup$
– Lukas Lang
1 hour ago










3 Answers
3






active

oldest

votes


















5












$begingroup$

Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a SparseArray uses rules to store the values and since ReplaceAll works with the FullForm of an expression care has to be taken (using Normal will make the array a regular matrix again).



This should work:



max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];

a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)

aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm


Matrix



Lukas Lang's suggestion is even nicer:



aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;





share|improve this answer











$endgroup$













  • $begingroup$
    Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
    $endgroup$
    – Henrik Schumacher
    26 mins ago





















2












$begingroup$

a cannot be a MatrixForm:



(a = KroneckerProduct[PotentialTilde[1], 
PotentialTilde[1]]) // MatrixForm


Convert to a regular Matrix:



Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}

(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a

MatrixForm[a]





share|improve this answer









$endgroup$





















    2












    $begingroup$

    A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
    B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
    B // MatrixForm







    share|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "387"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fmathematica.stackexchange.com%2fquestions%2f189758%2fset-every-element-to-zero-in-matrix-unless-its-1-or-1-2%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5












      $begingroup$

      Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a SparseArray uses rules to store the values and since ReplaceAll works with the FullForm of an expression care has to be taken (using Normal will make the array a regular matrix again).



      This should work:



      max = 1;
      PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
      Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];

      a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
      (* a // MatrixForm *)

      aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
      aMod // MatrixForm


      Matrix



      Lukas Lang's suggestion is even nicer:



      aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;





      share|improve this answer











      $endgroup$













      • $begingroup$
        Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
        $endgroup$
        – Henrik Schumacher
        26 mins ago


















      5












      $begingroup$

      Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a SparseArray uses rules to store the values and since ReplaceAll works with the FullForm of an expression care has to be taken (using Normal will make the array a regular matrix again).



      This should work:



      max = 1;
      PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
      Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];

      a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
      (* a // MatrixForm *)

      aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
      aMod // MatrixForm


      Matrix



      Lukas Lang's suggestion is even nicer:



      aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;





      share|improve this answer











      $endgroup$













      • $begingroup$
        Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
        $endgroup$
        – Henrik Schumacher
        26 mins ago
















      5












      5








      5





      $begingroup$

      Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a SparseArray uses rules to store the values and since ReplaceAll works with the FullForm of an expression care has to be taken (using Normal will make the array a regular matrix again).



      This should work:



      max = 1;
      PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
      Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];

      a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
      (* a // MatrixForm *)

      aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
      aMod // MatrixForm


      Matrix



      Lukas Lang's suggestion is even nicer:



      aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;





      share|improve this answer











      $endgroup$



      Be careful with assigning a "form" (e.g. MatrixForm) to a variable. Another observation is that a is a SparseArray uses rules to store the values and since ReplaceAll works with the FullForm of an expression care has to be taken (using Normal will make the array a regular matrix again).



      This should work:



      max = 1;
      PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
      Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];

      a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
      (* a // MatrixForm *)

      aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
      aMod // MatrixForm


      Matrix



      Lukas Lang's suggestion is even nicer:



      aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 48 mins ago

























      answered 1 hour ago









      gwrgwr

      7,81322558




      7,81322558












      • $begingroup$
        Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
        $endgroup$
        – Henrik Schumacher
        26 mins ago




















      • $begingroup$
        Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
        $endgroup$
        – Henrik Schumacher
        26 mins ago


















      $begingroup$
      Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
      $endgroup$
      – Henrik Schumacher
      26 mins ago






      $begingroup$
      Huh, never apply Normal to SparseArrays. Hell might break loose... ;) (Anyways, +1 of course.)
      $endgroup$
      – Henrik Schumacher
      26 mins ago













      2












      $begingroup$

      a cannot be a MatrixForm:



      (a = KroneckerProduct[PotentialTilde[1], 
      PotentialTilde[1]]) // MatrixForm


      Convert to a regular Matrix:



      Normal[a] /. {ij_ /; (NumberQ[
      ij] && ( ij != 1/2 && ij != 1)) :> 0}

      (a = Normal[
      a] /. {ij_ /; (NumberQ[
      ij] && ( ij != 1/2 && ij != 1)) :>
      0}) // MatrixForm
      a

      MatrixForm[a]





      share|improve this answer









      $endgroup$


















        2












        $begingroup$

        a cannot be a MatrixForm:



        (a = KroneckerProduct[PotentialTilde[1], 
        PotentialTilde[1]]) // MatrixForm


        Convert to a regular Matrix:



        Normal[a] /. {ij_ /; (NumberQ[
        ij] && ( ij != 1/2 && ij != 1)) :> 0}

        (a = Normal[
        a] /. {ij_ /; (NumberQ[
        ij] && ( ij != 1/2 && ij != 1)) :>
        0}) // MatrixForm
        a

        MatrixForm[a]





        share|improve this answer









        $endgroup$
















          2












          2








          2





          $begingroup$

          a cannot be a MatrixForm:



          (a = KroneckerProduct[PotentialTilde[1], 
          PotentialTilde[1]]) // MatrixForm


          Convert to a regular Matrix:



          Normal[a] /. {ij_ /; (NumberQ[
          ij] && ( ij != 1/2 && ij != 1)) :> 0}

          (a = Normal[
          a] /. {ij_ /; (NumberQ[
          ij] && ( ij != 1/2 && ij != 1)) :>
          0}) // MatrixForm
          a

          MatrixForm[a]





          share|improve this answer









          $endgroup$



          a cannot be a MatrixForm:



          (a = KroneckerProduct[PotentialTilde[1], 
          PotentialTilde[1]]) // MatrixForm


          Convert to a regular Matrix:



          Normal[a] /. {ij_ /; (NumberQ[
          ij] && ( ij != 1/2 && ij != 1)) :> 0}

          (a = Normal[
          a] /. {ij_ /; (NumberQ[
          ij] && ( ij != 1/2 && ij != 1)) :>
          0}) // MatrixForm
          a

          MatrixForm[a]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 40 mins ago









          Craig CarterCraig Carter

          499412




          499412























              2












              $begingroup$

              A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
              B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
              B // MatrixForm







              share|improve this answer











              $endgroup$


















                2












                $begingroup$

                A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
                B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
                B // MatrixForm







                share|improve this answer











                $endgroup$
















                  2












                  2








                  2





                  $begingroup$

                  A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
                  B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
                  B // MatrixForm







                  share|improve this answer











                  $endgroup$



                  A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
                  B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
                  B // MatrixForm








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 mins ago

























                  answered 33 mins ago









                  Henrik SchumacherHenrik Schumacher

                  50.5k469144




                  50.5k469144






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematica Stack Exchange!


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


                      Use MathJax to format equations. MathJax reference.


                      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%2fmathematica.stackexchange.com%2fquestions%2f189758%2fset-every-element-to-zero-in-matrix-unless-its-1-or-1-2%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

                      Polycentropodidae

                      Magento 2 Error message: Invalid state change requested

                      Paulmy