Announcement

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

  • A vector argument to sprintf

    Hi
    I've become quite fond of using sprintf.
    And increasingly often I find myself with a vector of arguments I have to split up in a tedious way.

    So far as I can get with my limited ressources the only thing I can do in Stata is:
    Code:
    v = 12,23,31
    sprintf("%f, %f, %f", v[1], v[2], v[3])
    Is there a smart way of doing something like:
    Code:
    sprintf("%f, %f, %f", v)
    Kind regards

    nhb

  • #2
    Maybe you could try mm_matlist() function from the moremata package (author: Benn Jann) , if the purpose is to control the format of your vector or matrix.

    Comment


    • #3
      Hi Christophe
      Thank you.
      A fine suggestion and I will definitely look into it.
      But I also often use string vectors/matrices.
      I admit that I didn't provide a proper example, sorry.
      Is there something similar for string vectors/matrices?
      Kind regards

      nhb

      Comment


      • #4
        ok, I overlooked the string part of your post. Isn't there some inspiration you could get from
        mm_matlist()'s code?

        Comment


        • #5
          Probably
          I'll look into it.
          The documentation says it uses -sprintf-, so maybe I'm lucky.
          Kind regards

          nhb

          Comment


          • #6
            Has anyone yet created a version of Ben Jann 's mm_matlist() that would accept a string, rather than real, matrix?
            Last edited by Mead Over; 04 Dec 2024, 12:49.

            Comment


            • #7
              re: #6, I would often find this useful.

              I occasionally use a brute-force method based on my user-written function invtokmatstr along with Mata's -display- function.
              Code:
              /* returns Nx1 matrix of rows' invtokens of Nxk string matrix z      */
              /* string scalar s defines the separators between the rows' elements */
              /* (see help invtokens)                                              */
              
              function invtokmatstr(z,|string scalar s) {
              string matrix c
              c=J(0,1,"")
              for (j=1;j<=rows(z);j++) {
               c=c\invtokens((z[j,.]),s)
              }
              return(c)
              }
              For example
              Code:
              mata
              
              sm=J(10,4,"abc")
              
              sm
              
              ism=invtokmatstr(sm, ", ")
              
              ism
              
              display(ism)
              
              end
              which yields
              Code:
              . mata
              ------------------------------------------------- mata (type end to exit) ---------------------------
              :
              : sm=J(10,4,"abc")
              
              :
              : sm
                        1     2     3     4
                   +-------------------------+
                 1 |  abc   abc   abc   abc  |
                 2 |  abc   abc   abc   abc  |
                 3 |  abc   abc   abc   abc  |
                 4 |  abc   abc   abc   abc  |
                 5 |  abc   abc   abc   abc  |
                 6 |  abc   abc   abc   abc  |
                 7 |  abc   abc   abc   abc  |
                 8 |  abc   abc   abc   abc  |
                 9 |  abc   abc   abc   abc  |
                10 |  abc   abc   abc   abc  |
                   +-------------------------+
              
              :
              : ism=invtokmatstr(sm, ", ")
              
              :
              : ism
                                       1
                   +----------------------+
                 1 |  abc, abc, abc, abc  |
                 2 |  abc, abc, abc, abc  |
                 3 |  abc, abc, abc, abc  |
                 4 |  abc, abc, abc, abc  |
                 5 |  abc, abc, abc, abc  |
                 6 |  abc, abc, abc, abc  |
                 7 |  abc, abc, abc, abc  |
                 8 |  abc, abc, abc, abc  |
                 9 |  abc, abc, abc, abc  |
                10 |  abc, abc, abc, abc  |
                   +----------------------+
              
              :
              : display(ism)
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              abc, abc, abc, abc
              
              :
              : end
              I have sometimes thought that expanding the capabilities of Mata's -display- function to accommodate general string matrix arguments instead of just string column vectors would be valuable.
              Last edited by John Mullahy; 05 Dec 2024, 09:37.

              Comment


              • #8
                Hi, coding invtokmatstr() as follows would be much more efficient.
                Code:
                mata:
                string colvector invtokmatstr(string matrix z, | string scalar s)
                {
                    real scalar      j
                    string colvector c
                    
                    j = rows(z)
                    c = J(j,1,"")
                    for (;j;j--) {
                        c[j] = invtokens((z[j,.]),s)
                    }
                    return(c)
                }
                end
                Setting up a matrix of the correct size upfront and then filling it in is much less work for the computer than creating the matrix over an over, each time adding a row.
                ben

                Comment


                • #9
                  Just a few minor tweaks were necessary to add support for string matrices in mm_matlist(). I will update moremata on GitHub tomorrow (need access to a different computer that still has some old Stata versions installed that are used to compile moremata).

                  Comment


                  • #10
                    The new moremata version with string support in mm_matlist() is now available on GitHub, type

                    Code:
                    . net install moremata, replace from(https://raw.githubusercontent.com/benjann/moremata/master/)
                    to update moremata on your system.

                    Don't know how useful string support in mm_matlist() really is, but here are some examples.


                    Code:
                    . mata
                    : sm = J(4,3,"abc")
                    
                    : sm
                             1     2     3
                        +-------------------+
                      1 |  abc   abc   abc  |
                      2 |  abc   abc   abc  |
                      3 |  abc   abc   abc  |
                      4 |  abc   abc   abc  |
                        +-------------------+
                    
                    : mm_matlist(sm)
                             1     2     3
                        +-------------------+
                      1 |  abc   abc   abc  |
                      2 |  abc   abc   abc  |
                      3 |  abc   abc   abc  |
                      4 |  abc   abc   abc  |
                        +-------------------+
                    
                    : mm_matlist(sm, "%s", 0)
                            1     2     3
                      1   abc   abc   abc
                      2   abc   abc   abc
                      3   abc   abc   abc
                      4   abc   abc   abc
                    
                    : mm_matlist(sm, "%s", 2)
                        |    1     2     3
                    ----+-----------------
                      1 |  abc   abc   abc
                      2 |  abc   abc   abc
                      3 |  abc   abc   abc
                      4 |  abc   abc   abc
                    
                    : mm_matlist(sm, "%~8s", 3, "row ":+strofreal(1::rows(sm)),
                    >     "column ":+strofreal(1..cols(sm)))
                    -----------------------------------------
                            |  column 1   column 2   column 3
                    --------+--------------------------------
                      row 1 |     abc        abc        abc  
                      row 2 |     abc        abc        abc  
                      row 3 |     abc        abc        abc  
                      row 4 |     abc        abc        abc  
                    -----------------------------------------
                    
                    : end
                    ben

                    Comment

                    Working...
                    X