Announcement

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

  • Unstacking Pairs

    Hi I need a small data management trick that i am assuming it is easier in Mata than Stata.

    assume we have that stacking format:

    Code:
    clear
    input int(i j) byte similarity
     9  43 25
    28  67 21
    28  43 26
    89 124 21
    89 160 21
    89 123 21
    89 167 21
    89 125 22
    89 161 22
    89 154 22
    89  90 22
    89 157 22
    89 162 23
    end
    expected output Mata Matrix would be:

    first column = unique i
    follows columns be the J pair, but descendant ordered by similarity from left to right
    9 43
    28 43 67
    89 162 157 90 ...
    thks in advance.


  • #2
    The result you show can be done with Stata commands, and the resulting data structure can be made into a Mata matrix:

    Code:
    clear
    input int(i j) byte similarity
     9  43 25
    28  67 21
    28  43 26
    89 124 21
    89 160 21
    89 123 21
    89 167 21
    89 125 22
    89 161 22
    89 154 22
    89  90 22
    89 157 22
    89 162 23
    end
    //
    // Order j within i by similarity, and capture this order in a variable.
    bysort i (similarity): gen int simord = _n
    drop similarity
    // Get the wide layout you want.
    reshape wide j, i(i) j(simord)
    list
    // Assuming you want a Mata matrix as an end in itself.
    mata X = st_data(., .)
    mata X

    Comment

    Working...
    X