Announcement

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

  • Matrix raised to a certain power

    Hi all,

    I am new to mata and have a question related to matrix manipulation in MATA. I have:
    • A = (1;2;3) 3 x 1 column vector
    • B = (1,2) 1 x 2 row vector
    My final output is a 3 x 2 matrix, where the first column is just A raised to the power of 1 and the second column is just A raised to the power of 2. In the latest version of Matlab, the code would have simply been A.^B or bsxfun(A,B,.^). I was wondering if there is any efficient method of coding this up. Thanks.

  • #2
    Code:
    mata
    A = (1,2,3)
    B = (1,2)
    (((colnonmissing(B))'*A):^B')'
    end
    Res.:

    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) -------------
    :
    : A = (1,2,3)
    
    :
    : B = (1,2)
    
    :
    : (((colnonmissing(B))'*A):^B')'
           1   2
        +---------+
      1 |  1   1  |
      2 |  2   4  |
      3 |  3   9  |
        +---------+
    
    :
    : end
    -------------------------------------------------------------------------------------------------------------

    Comment

    Working...
    X