Announcement

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

  • Using an if condition in a for loop

    I am currently working with Mata. The dataset in Mata currently contains vectors A and B of dimension N*1 each. In summary, what I am trying to do is to :
    1. create another vector of random terms of the same dimension drawn from a normal distribution
    2. Sum element wise this vector to vectors A and B
    3. Evaluate if the sum is greater than 1 or less than 0
    4. If 3) is true for a particular element , redraw the random term till the condition holds
    5. Do this for all terms until each element of this vector which is the sum of A, B and the error term is either less than 1 or greater than 0. The following is the code I have written till now
    Code:
    putmata A B \\this inputs these vectors in mata
    Sum=J(rows(A), 1,0) \\\Creates a vector with each element 0- to loop over.
    for (i=1;i<=rows(A);i++) {
    Random[i,1] = rnormal(1,1,0,1)
    Sum[i,1]=A[i,1]+B[i,1]+Random[i,1]
    while (Sum[i,1]>1) {
    Random[i,1]= rnormal(1,1,0,1))
    }
    
    }
    end
    !
    This does not work- I still get values greater than 1 and less than 0.
    I think I see the problem, but not the syntax. There should be another condition here to keep on doing the drawing of the term till the statement is true. I am not sure if the while statement enforces this. Any help is greatly appreciated!
    Last edited by Chinmay Sharma; 23 Jun 2016, 10:38.

  • #2
    Just as an addition, the dataset I have is as follows:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(A B)
    .2  .5
     0  .6
    .3 .21
    .5  .4
    .6  .5
    .1  .1
    .2  .5
    end

    Comment


    • #3
      Thanks to a suggestion on a different forum, I have been able to produce the desired result. The correct code is as follows:


      putmata A B \\this inputs these vectors in mata Sum=J(rows(A), 1,0) \\\Creates a vector with each element 0- to loop over. for (i=1;i<=rows(A);i++) { Random[i,1] = rnormal(1,1,0,1) Sum[i,1]=A[i,1]+B[i,1]+Random[i,1] while (Sum[i,1]>1) { Random[i,1]= rnormal(1,1,0,1) Sum[i,1]=A[i,1]+B[i,1]+Random[i,1] } } end

      Comment


      • #4
        Indeed; you cross-posted at http://stackoverflow.com/questions/3...r-loop-on-mata

        Our request is explicit at http://www.statalist.org/forums/help#crossposting

        8. May I cross-post to other forums?

        People posting on Statalist may also post the same question on other listservers or in web forums. There is absolutely no rule against doing that.
        But if you do post elsewhere, we ask that you provide cross-references in URL form to searchable archives. That way, people interested in your question can quickly check what has been said elsewhere and avoid posting similar comments. Being open about cross-posting saves everyone time.
        If your question was answered well elsewhere, please post a cross-reference to that answer on Statalist.

        Comment

        Working...
        X