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   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Minimum of a vector or a matrix.
Date   Fri, 11 Nov 2005 15:07:39 -0000

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