Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Minimum of a vector or a matrix.


From   [email protected]
To   [email protected]
Subject   st: RE: Minimum of a vector or a matrix.
Date   Fri, 11 Nov 2005 10:51:11 -0500

Thanks Nick for the answers.
Indeed, mata is a great tool worth learning.
However, I do not get exactly what I want.
It is giving me "values" while I want "conditions at the place of
occurrences".

I've got:

       1   2   3
    +-------------+
  1 |  1   1   3  |
  2 |  1   1   3  |
  3 |  1   1   3  |
    +-------------+

while I wanted:

       1   2   3
    +-------------+
  1 |  1   0   1  |
  2 |  0   0   1  |
  3 |  0   1   0  |
    +-------------+

I tried to dig a little bit, but am not yet familiar with
looping in mata.

clear
mata:
A = (1,2,3\1,2,3\3,1,5)
r = cols(A)
B = colmin(A)\colmin(A)\colmin(A)
J = J(r,r,0)
A
r
B
J

for (i=1;i<=r;++i {
    J(i,j) = cond([A(i,j)==colmin(A),1,0)
}
J


I keep getting a conformability error.

Many thanks in advance.

Amadou.


-------------------------------------------------------------------------------------------------


                                                                                 
                                                                                 
                                                                                 
 I'll take these questions in different order.                                   
                                                                                 
 Nick                                                                            
 [email protected]                                                            
                                                                                 
 [email protected]                                                          
                                                                                 
 > Suppose the macro:                                                            
 > local mymacro 1 2 3 4 5 6 7 8 9 0                                             
 > how to localise and index the smallest value of the macro?                    
 > I checked with macrolists but there is no answer.                             
                                                                                 
 numlist "`mymacro'", sort                                                       
 local min : word 1 of `r(numlist)'                                              
                                                                                 
 > Is there a way to get the minimum value of a vector or a matrix?              
                                                                                 
  mata:                                                                          
 ------------------------------------------------- mata (type end to             
 exit) --------------------                                                      
 : m = (1,2,3\4,5,6\7,8,9)                                                       
                                                                                 
 : min(m)                                                                        
   1                                                                             
                                                                                 
 : rowmin(m)                                                                     
        1                                                                        
     +-----+                                                                     
   1 |  1  |                                                                     
   2 |  4  |                                                                     
   3 |  7  |                                                                     
     +-----+                                                                     
                                                                                 
 : colmin(m)                                                                     
        1   2   3                                                                
     +-------------+                                                             
   1 |  1   2   3  |                                                             
     +-------------+                                                             
                                                                                 
 > Suppose :                                                                     
 >                                                                               
 >                                                                               
 > A[3,3]                                                                        
 >     c1  c2  c3                                                                
 > r1   0   2   3                                                                
 > r2   1   2   3                                                                
 > r3   3   1   5                                                                
 >                                                                               
 > I want to have:                                                               
 >                                                                               
 > B[3,3]                                                                        
 >     c1  c2  c3                                                                
 > r1   1   0   1                                                                
 > r2   0   0   1                                                                
 > r3   0   1   0                                                                
 >                                                                               
 > where the 1s are true if the value in column j is the smallest.               
                                                                                 
 : m :== (rowmin(m), rowmin(m), rowmin(m))                                       
        1   2   3                                                                
     +-------------+                                                             
   1 |  1   0   0  |                                                             
   2 |  1   0   0  |                                                             
   3 |  1   0   0  |                                                             
     +-------------+                                                             
                                                                                 
 : m :== (colmin(m) \ colmin(m) \ colmin(m))                                     
        1   2   3                                                                
     +-------------+                                                             
   1 |  1   1   1  |                                                             
   2 |  0   0   0  |                                                             
   3 |  0   0   0  |                                                             
     +-------------+                                                             
                                                                                 
 > Is there a way to combine (ie: the smallest value of the column               
 > j AND of the corresponding line i)?                                           
                                                                                 
 This is one is left as an exercise.                                             
                                                                                 
                                                                                 
 ------------------------------------------------------------------------------- 


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index