Announcement

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

  • Matrix row calculations

    Hey everybody,
    once again im asking for your help! Could you please tell me how to multiply the elements of a row with each other such as rowsum() just for multiplication then.
    Thanks guys!

  • #2
    Don Corleone If that is your real name, I advise (for once) using a pseudonym, as my guess is that most of your questions are being ignored because people guess that you are not serious about our firm and explicit request to use full real names.

    If that is not your real name, I advise use of a full real name.

    This is all explained at length at https://www.statalist.org/forums/help#realnames and #3 of https://www.statalist.org/forums/help#adviceextras

    In terms of your question I guess you want the row product. If all values are all positive then just use logarithms first, sum, and invert the transformation after. Otherwise you may have to loop, but that is not especially difficult.


    Code:
    : x = (1,2,3\4,5,6\7,8,9)
    
    : exp(rowsum(log(x)))
             1
        +-------+
      1 |    6  |
      2 |  120  |
      3 |  504  |
        +-------+
    
    : sum = J(rows(x), 1, 1)
    
    : for(j = 1; j <= cols(x); j++) sum = sum :* x[,j]
    
    : sum
             1
        +-------+
      1 |    6  |
      2 |  120  |
      3 |  504  |
        +-------+

    Comment


    • #3
      First of all thanks a lot!
      I didnt know that real names are prefered- my mistake. Just sent a request to change that.
      All the best
      Constantin

      Comment


      • #4
        When i use
        exp(rowsum(log(x))) this already seems to be the full calculation i needed, thats why i wonder about the second step. Is this important for my task?

        Comment


        • #5
          And yet another question that i struggle with. From the matrix x you created, how can i get the nth root out of every value?
          i already know hot to do it with the squareroot but thats it.

          Comment


          • #6
            You can edit your previous question within one hour of posting. This and much else is explained in the FAQ Advice all posters are prompted to read before posting. https://www.statalist.org/forums/help

            #4 is already answered in #2. Here I add emphasis.

            If all values are all positive (is that true in your case?) then just use logarithms first, sum, and invert the transformation after.


            Otherwise -- if any value in your matrices is zero or negative -- you may have to loop, but that is not especially difficult.
            #5 nth roots are just powers.

            Code:
             x = (1,2,3\4,5,6\7,8,9)
            
            : x:^(1/3)
                             1             2             3
                +-------------------------------------------+
              1 |            1    1.25992105    1.44224957  |
              2 |  1.587401052   1.709975947   1.817120593  |
              3 |  1.912931183             2   2.080083823  |
                +-------------------------------------------+
            Again, for zeros or negative values either some roots are not defined or you may need more elaborate code. For example, the cube root is defined for negative values too, but the calculation needs more care.

            Code:
            : x = -x
            
            : sign(x) :* abs(x):^(1/3)
                              1              2              3
                +----------------------------------------------+
              1 |            -1    -1.25992105    -1.44224957  |
              2 |  -1.587401052   -1.709975947   -1.817120593  |
              3 |  -1.912931183             -2   -2.080083823  |
                +----------------------------------------------+

            Comment


            • #7
              Very very helpful, thank you!
              Have a nice weekend

              Comment

              Working...
              X