Announcement

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

  • What is wrong with this code

    Dear all,

    what is wrong with this code:

    clear mata
    mata

    function task3(real vector x) {

    real scalar i
    real scalar n
    real scalar r
    real scalar c

    r = rows(x)
    c = cols(x)

    if (c == 1) {
    n = r
    X=J(1,1,.)
    }
    if (r == 1) {
    n = c
    Y=J(1,1,.)
    }
    for(i=1; i<=n; i = i + 2) {
    X = sum(i+i)
    Y = sum(i+i)
    }

    return(X)
    return(Y)
    }

    x = (1,2,4,7,5,6)
    task3(x)

    end




    If I add or change numbers, this code is not working.

    Thank you.

  • #2
    Several issues AFAIK:
    • Not sure why you do sum(i+i) and not just i+i
    • You can't have two returns
    • Why X and Y as 1x1 matrices instead of just scalars?
    • You never use x in the function
    • Why not use length() instead of rows() and cols() and the lengthy conditional to determine n?

    Comment


    • #3
      Seems to be a variant on http://www.statalist.org/forums/foru...-mata-function. The question there is more informative.

      Comment

      Working...
      X