Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: MATA all combinations / pairs of a row


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: MATA all combinations / pairs of a row
Date   Fri, 7 Oct 2011 23:37:34 +0100

I'd check out books on combinatorial algorithms.

Nick

On Fri, Oct 7, 2011 at 11:28 PM, Sebastian Eppner <[email protected]> wrote:
> Hi,
> yes, based on the select and rowsum = 2 logic I wrote this
> (unfortunately it has to use one loop... first I thought that may be
> ok... in reality in really is time consuming... if there are ANY ideas
> out there how to prevent the loop, i' be very grateful).
>
>
>
> BASIC = ( 1,1,1) \ (1,1,0) \ (1,0,1) \ (0,1,1) \ (0,0,1) \ (0,1,0) \ (1,0,0)
> PROPERTIES = (0,9,10) \ (0.45, 0.2, 0.35)
>
>
> PROP1 = PROPERTIES[1,.]
> PROP1 = diag(PROP1)
> PROP1 = BASIC * PROP1
>
> PROP2 = PROPERTIES[2,.]
> PROP2 = diag(PROP2)
> PROP2 = BASIC * PROP2
>
> Now I have 3 Matrices with all the information i needed. The problem was now:
>
>> For each row/combination in BASIC  I need to  find all PAIRS of Ones
>> that are contained in the combination. For any pair, I need to
>> multiply:
>> PROP1 of Element1 * PROP1 of Element2  * abs(PROP2 of Element1-PROP2
>> of ELEMENT2)
>
> I calculated the latter not only for pairs, but for all rows (later i
> only use the pair rows)
>
> PAIRS =  rowmax(PROP1):*rowmin(PROP1):*(rowmax(PROP2)-rowmin(PROP2))
>
> Then I calculate the sum of all pairs contained in the first row. see
> the select() which only selects those rows that are:
> - contain only elements that are also elements in row one of BASICS
> (that i do by subtracting all rows from the first one, and then I look
> if there is a -1 somewhere)
> - contain only pairs (as Nick suggested that means the rowsum is 2)
>
> mata: result = colsum(select(PAIRS, (rowmin(BASIC[1,.]:-BASIC):>-1):&
> rowsum(BASIC):==2))
>
> of course i need the result for all the other rows too! thats where i
> cant think of anything but a loop (****jsdf)
>
> forvalues i=2/`lastrow' {
> mata: result = result \ colsum(select(PAIRS,
> (rowmin(BASIC[`i',.]:-BASIC):>-1):& rowsum(BASIC):==2))
> }
>
> any suggestion would be magic.
> Thanks,
> Sebastian
>
> On Thu, Oct 6, 2011 at 2:16 AM, Nick Cox <[email protected]> wrote:
>> One idea only from me. Look at -select()- and think of rowsums being 2.
>>
>> Nick
>>
>> On Wed, Oct 5, 2011 at 10:08 PM, Sebastian Eppner <[email protected]> wrote:
>>> Hi,
>>> I have N elements. In this example N will be 3. But in my application
>>> it will be usually around 7, going up to 20 in some cases…
>>>
>>> Each element can be either 1 or 0. My basic matrix has all 2^N, here
>>> 2^3 permutations of the elements:
>>>
>>> BASIC = ( 1,1,1) \ (1,1,0) \ (1,0,1) \ (0,1,1) \ (0,0,1) \ (0,1,0) \
>>> (1,0,0) \(0,0,0)
>>>
>>> The elements have 2 “properties” , PROP1 and PROP2  each, stored in 2
>>> rows of another matrix, for example:
>>>
>>> PROPERTIES = (1.4,4.6,8.1) \ (0.2, 0.4, 0.4)
>>>
>>> Every row in BASIC resembles a unique combination of the elements.
>>> What I want to do, is to calculate some indicators for every
>>> combination, using the Properties of the elements.
>>>
>>> An easy example would be to calculate the total sum of PROP1 for each
>>> combination. I just need to multiply the “diagonalized” first row of
>>> the PROPERTIES Matrix with the BASIC Matrix and then calculate the
>>> rowsum of this resulting matrix.
>>>
>>>
>>> What I want to do now is more complex and I am really stuck. The
>>> indicator I want to calculate now is the following:
>>>
>>> For each row/combination in BASIC  I need to  find all PAIRS of Ones
>>> that are contained in the combination. For any pair, I need to
>>> multiply:
>>>
>>> PROP1 of Element1 * PROP1 of Element2  * abs(PROP2 of Element1-PROP2
>>> of ELEMENT2)
>>>
>>> It is the sum of all PAIRS that I need to know for every row….
>>>
>>>
>>> Of course, not every row has PAIRS in it… all rows with only 1 One
>>> have no pairs, so the result for these rows should be 0 (or missing, I
>>> don’t care).
>>>
>>> Also, many rows (those with only 2 Ones) have only a single Pair…
>>>
>>> In my Example with 3 elements, only the first row sums up more than
>>> one (3) Pair(s)….
>>>
>>>
>>> I am looking for a way to solve this problem by not using loops… and
>>> as much matrix algorithms as possible. Since I have a lot of BASIC
>>> Matrices with more elements than 3… I guess the whole thing would be
>>> very very time consuming if I started smth with loops… Maybe there is
>>> no way to get rid of any loop… any idea would be very welcome (even
>>> some, with a little looping).
>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index