# st: Fw: Minimum of a vector or a matrix.

 From adiallo5@worldbank.org To statalist@hsphsun2.harvard.edu Subject st: Fw: Minimum of a vector or a matrix. Date Fri, 11 Nov 2005 14:46:39 -0500

```Dear list.
Courtesy of Kerry Kamirre,
Here is the answer to my previous posting.
Mata is really great.
I'd like to thank both Nick Cox and Kerry.
Have a wonderful weekend.

----- Forwarded by Amadou Bassirou Diallo/Person/World Bank on 11/11/2005 02:43
PM -----

tech-support
Sent by:
ktk@stata.com                                                   To
cc
11/11/2005
12:36 PM                                                   Subject
Re: Minimum of a vector or a matrix.

There isn't any looping needed to perform these calculations. This can all be
done using the ":" operators. Here is an example of the syntax:

--begin log--

: A = (0,2,3\1,2,3\3,1,5)

: B = colmin(A)\colmin(A)\colmin(A)

: C = B:==A

: C
1   2   3
+-------------+
1 |  1   0   1  |
2 |  0   0   1  |
3 |  0   1   0  |
+-------------+

: B = rowmin(A),rowmin(A),rowmin(A)

: D = B:==A

: D
1   2   3
+-------------+
1 |  1   0   0  |
2 |  1   0   0  |
3 |  0   1   0  |
+-------------+

: E = C:&D

: E
1   2   3
+-------------+
1 |  1   0   0  |
2 |  0   0   0  |
3 |  0   1   0  |
+-------------+

--end of log--

Sincerely,

Kerry Kammire

************************************************************************

___  ____  ____  ____  ____ tm
/__    /   ____/   /   ____/
___/   /   /___/   /   /___/     Kerry Kammire
Statistics/Data Analysis    Technical Services
tech-support@stata.com
StataCorp LP
4905 Lakeway Drive
College Station, TX 77845

Interested in learning more about Stata?  Check out our Web based
course offerings.

http://www.stata.com/info/products/netcourse/

************************************************************************

You wrote:

-----Begin Original Message-----
Hi,
I've checked with the statalist but I need a rather urgent answer
to this question.
Nick Cox pointed me in a direction but I cannot take from there
and get to the final solution.
Here is my question.
Suppose you have a matrix:

: A
1   2   3
+-------------+
1 |  0   2   3  |
2 |  1   2   3  |
3 |  3   1   5  |
+-------------+

I want a true condition being placed on each element of the 3 columns.
Element is one 1 if it is the smallest value of the column, and 0 otherwise.

So I want:

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

My other question is:
Element (i,j) is 1 if  it is the smallest value of the column j AND the column i
, and 0 otherwise.
I want:

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

Here is what I coded, but I do not know how to implement double loops in mata.

clear
cap mata clear
mata:
A = (0,2,3\1,2,3\3,1,5)
r = cols(A)
A
r
B = colmin(A)\colmin(A)\colmin(A)
B
J = J(r,r,0)
J
for (i=1;i<=r;++i {
J[i,j] = cond([A[i,j]==colmin(A),1,0)
}
J

Best regards.