Announcement

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

  • changing elements in a matrix according to [row,column] coordinates in another matrix

    Dear statalist,

    I have a matrix "X" of zeroes of dimensions (r by k).

    I have a second matrix "C" of dimensions (n by 2), where each row denotes a position in "X" where I want to replace a zero for a 1.

    For example, the first three rows in "C" are:

    1 3
    4 24
    6 39

    I want to replace into ones the zeroes in positions [1,3], [4,2] and [6,39] in "X" .

    I am using Stata version 13.1.
    Thank you very much
    Jose Uribe

  • #2
    Code:
    for (i=1; i<=rows(C); i++) {
        X[C[i,1],C[i,2]] = 1
    }

    Comment


    • #3
      Thanks Phil, that is exactly what I needed!

      Comment


      • #4
        It should also work and avoids the loop
        Code:
        k = max( (rows(C),cols(C)))
        X[C[,1],C[,2]] = J(k,k,1)
        Best
        Christophe

        Comment


        • #5
          Sorry for misleading you with my last post, since it does not give the intended result. So forget it.

          Comment


          • #6
            ok last try :-).
            Code:
            k = max( (rows(C),cols(C)))
            X[C[,1],C[,2]] = I(k)

            Comment


            • #7
              Christophe, your code is appealing but I did not get the desired answer (as I did with the loop).
              I have another question, intimately related to the first:
              After some operations on X, I now have matrix Y, also of dimensions (r by k).

              I want to "extract" only those entries in Y indexed by the coordinates in matrix C dimension (n by 2) and append them to matrix C to create matrix W dimension (n by 3) .

              For example, the first three rows in "C" dimension (n by 2) are:

              1 3
              4 24
              6 39

              Let's say the values in Y indexed by [1,3], [4,2] and [6,39] are 0, 5 and 10.

              The resulting matrix W should be

              1 3 0
              4 24 5
              6 39 10



              Thank you very much
              Jose Uribe

              Comment


              • #8
                Originally posted by Jose Uribe View Post
                Christophe, your code is appealing but I did not get the desired answer (as I did with the loop).
                Christophe's approach is neat, but it depends on assumptions about C (i.e., that it specifies at most one cell per row of X) that may not be true. I presume that's why it didn't yield the correct result in your case.

                All is not lost, however; we can use Christophe's suggested use of subscripts to solve your second question (and nicely so):
                Code:
                W = C, diagonal(Y[C[,1],C[,2]])
                Note that while it's perfectly acceptable to ask narrow questions like this, stitching together a bunch of code fragments is unlikely to result in an efficient overall solution. That may well not matter in your case; indeed, with the speed of modern computers, it often doesn't. However, you might also consider describing more of the problem you are trying to solve, which might permit people to suggest a more efficient (and/or simpler) overall approach. Also, if you're going to work in Mata (and you definitely should!), spending an afternoon going through selected sections of the Mata Reference Manual is an invaluable investment.
                Last edited by Phil Schumm; 26 Apr 2014, 15:56.

                Comment


                • #9
                  Thanks very much Phil. The code works like a charm. I have and will continue to study the Mata Reference Manual.
                  Best,
                  Jose

                  Comment

                  Working...
                  X