Min function

14 views
Skip to first unread message

sahebe...@gmail.com

unread,
Oct 18, 2016, 6:25:25 PM10/18/16
to AMPL Modeling Language
Dear all
I have a matrix[i,j]. How can i find the number of rows of the minimum value???!
For example: I want to get number 2 because 5 is in it.
Thank alot
1 2 3

1 10 40 50

2 5 70 15

3 10 15 25

Robert Fourer

unread,
Oct 19, 2016, 11:25:31 AM10/19/16
to am...@googlegroups.com
These statements define param minrow as the number of the first row in which the minimum value occurs:

set S = 1..3;
param matrix {S,S};

param minval = min {i in S, j in S} matrix[i,j];
param minrow = min {i in S: exists {j in S} matrix[i,j] = minval} i;

This is not especially efficient, but you will not notice the inefficiency unless you have a very large matrix -- say, more than 1000 rows. For a matrix of 10000 rows, the following is faster:

param minval = min {i in S, j in S} matrix[i,j];
param minrowval {i in S} = min {j in S} matrix[i,j];
param minrow = min {i in S: minrowval[i] = minval} i;

But here you are likely to spend more time just setting up the matrix, since it has 100 million elements.

Bob Fourer
am...@googlegroups.com


-----Original Message-----
From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Tuesday, October 18, 2016 5:25 PM
To: AMPL Modeling Language
Subject: [AMPL 12766] Min function

I have a matrix[i,j]. How can i find the number of rows of the minimum value???!
For example: I want to get number 2 because 5 is in it.

Reply all
Reply to author
Forward
0 new messages