Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass the scalar to arguments of the mata function?

    Dear colleagues:

    I run the following program. However, following error appeared.

    . mata: wx("distance_smat", "y_m", "hage_0", 2);
    wx(): 3301 subscript invalid
    <istmt>: - function returned error


    I assume that there is a problem when I pass the real scalar "2" to the argument of void function wx.

    It would be great if somebody suggested the way to correct the error.

    Thanks in advance.

    Kazuto

    Code:
    #delimit;
    /*** mata session ***/
    
    
    mata;
    void wx(string scalar A, string scalar B, string scalar C, real scalar d_km){; 
    
        /* Obtain and put Stata matrices */
        distance_mat = st_matrix(A);
    
        x_mat = st_matrix(B);
    
        if_mat = st_matrix(C); 
    
        nob = rows(distance_mat);
    
        dstm1 = J(nob, nob, 0); 
        dstm2 = J(nob, nob, 0); 
        dstm1_invdist= J(nob, nob, 0); 
    
    
        for (i=1; i<=nob; i++) {;
            k= i+1;
            for (j=k; j<=nob; j++) {;
                if (distance_mat[i,j]<d_km) {;    
                    dstm1[i,j] = 1;
                    dstm1_invdist = 1/distance_mat[i,j];
                };
            };
        };
    
        /* save */
        st_matrix("w1_x", w1_x);
        st_matrix("w_invdist_x", w_invdist_x);
    
        st_matrix("nb_ratio", nb_ratio);
    };
    end;
    
    /**** stata part ***/
    matrix distance_smat = distance_mat+distance_mat';
    
    mkmat lease_rate_kanri, matrix(y_m);
    
    mkmat hage_0 , matrix(hage_0_v);
    
    mata: wx("distance_smat", "y_m", "hage_0", 2);

  • #2
    Originally posted by Kazuto Sumita View Post
    . . . following error appeared.

    . . . wx(): 3301 subscript invalid . . .

    I assume that there is a problem when I pass the real scalar "2" to the argument of void function wx.
    No.

    When i is equal to nob, j is equal to nob + 1 and distance_mat[i,j] etc. have an invalid subscript.

    Comment


    • #3
      Dear Joseph:
      Thanks a lot!
      Kazuto

      Comment


      • #4
        I changed the loop parts as below:
        Code:
         
            for (i=1; i<=nob-1; i++) {;         k= i+1;         for (j=k; j<=nob; j++) {;             if (distance_mat[i,j]<d_km) {;                     dstm1[i,j] = 1;                 dstm1_invdist[i,j] = 1/distance_mat[i,j];             };          };     };
        But I have the same error.

        Code:
        mata: wx("distance_smat", "y_m", "hage_0", 2);
                            wx():  3301  subscript invalid
                         <istmt>:     -  function returned error
        r(3301);

        Comment


        • #5
          Actually, I agreed with Joseph's comment in post #2, but a closer look at the syntax description of the for statement tells us that when j is set to nob+1 the contents of the for loop over j will not be executed. So the problem must lie elsewhere.

          However, I am not seeing where the problem might be. The following stripped-down version of your code runs without an error, and yet there are no obvious subscripts elsewhere in your code.
          Code:
          #delimit;
          /*** mata session ***/
          
          
          mata;
          mata drop wx()
          void wx(real scalar d_km){;
          
              nob = 4;
          
              distance_mat = J(nob, nob, 42)
              
              dstm1 = J(nob, nob, 0);
              dstm1_invdist= J(nob, nob, 0);
          
          
              for (i=1; i<=nob; i++) {;
                  k= i+1;
                  for (j=k; j<=nob; j++) {;
                      if (distance_mat[i,j]<d_km) {;    
                          dstm1[i,j] = 1;
                          dstm1_invdist[i,j] = 1/distance_mat[i,j];
                      };
                  };
              };
          
          };
          end;
          
          /**** stata part ***/
          
          mata: wx(666);
          I am not convinced you have shown the actual code you ran, because in
          Code:
              /* save */
              st_matrix("w1_x", w1_x);
              st_matrix("w_invdist_x", w_invdist_x);
          
              st_matrix("nb_ratio", nb_ratio);
          you save three matrices that were nowhere created, and because in
          Code:
          dstm1_invdist = 1/distance_mat[i,j];
          you lack subscripts on the matrix dstm1_invdist.

          Note that I added mata drop wx() to the code: is is possible your redefinitions of wx() are being ignored because wx() already exists from an earlier invocation in your session?
          Last edited by William Lisowski; 28 Feb 2018, 09:28.

          Comment


          • #6
            Dear William

            I was careless to forget the subscripts.

            I noticed that I mistook the name of the variable in the argument and correct the following parts from
            Code:
            mata: wx("distance_smat", "y_m", "hage_0", 2);
            to
            Code:
            mata: wx("distance_smat", "y_m", "hage_0_v", 2);
            After correcting those, program worked successfully.

            Thanks for your comments!

            Kazuto

            Code:
            #delimit;
            /*** mata session ***/
            
            mata;
            void wx(string scalar A, string scalar B, string scalar C, real scalar d_km){; 
            
                /* Obtain and put Stata matrices */
                distance_mat = st_matrix(A);
            
                x_mat = st_matrix(B);
            
                if_mat = st_matrix(C); 
            
                nob = rows(distance_mat);
            
                dstm1 = J(nob, nob, 0); 
                dstm2 = J(nob, nob, 0); 
                dstm1_invdist= J(nob, nob, 0); 
            
            
                for (i=1; i<=nob-1; i++) {;
                    k= i+1;
                    for (j=k; j<=nob; j++) {;
                        if (distance_mat[i,j]<d_km) {;    
                            dstm1[i,j] = 1;
                            dstm1_invdist[i,j] = 1/distance_mat[i,j];
                        };
                    };
                };
            
                /* save */
                st_matrix("w1_x", w1_x);
                st_matrix("w_invdist_x", w_invdist_x);
            
                st_matrix("nb_ratio", nb_ratio);
            };
            end;
            
            /**** stata part ***/
            matrix distance_smat = distance_mat+distance_mat';
            
            mkmat lease_rate_kanri, matrix(y_m);
            
            mkmat hage_0 , matrix(hage_0_v);
            
            mata: wx("distance_smat", "y_m", "hage_0_v", 2);

            Comment


            • #7
              Yeah,
              Code:
              for (j=k; j<=nob; j++) {
              won't execute when j starts out greater than nob.

              A couple of observations.

              1. The error apparently is akin to this:
              Code:
              clear *
              
              mata:
              mata set matastrict on
              
              void function test1(string scalar matrix_name) {
                  real matrix MyMatrix
                  MyMatrix = st_matrix(matrix_name)
                  MyMatrix
              }
              
              test1("NoSuchMatrix")
              
              end
              
              exit
              That executes without any error. I would have expected some kind of "Stata matrix not found" kind of error.

              2. This does give an error:
              Code:
              mata:
              mata clear
              // mata set matastrict on
              
              void function test2() {
                  st_matrix("w1_x", w1_x)
              }
              
              test2()
              
              end
              
              exit
              So, as William observed, where do those Mata matrices come from? Are they global?

              Comment

              Working...
              X