# How do you program a large table and search the minimum in each column? [closed]

How does one program a table (2d list) with a huge amount of data which can be done manually. For example: the rows go from -1 to 1 by step 0.0005 and the columns go from -3 to 3 by step 1. Subsequently, how does one search the minimum value in each column and print the pair of values (column's value,row's value) what is corresponding to the column's minimum value?

## closed as off-topic by ciao, MarcoB, Öskå, ilian, yohbsSep 20 '15 at 16:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – ciao, MarcoB, Öskå, ilian, yohbs
If this question can be reworded to fit the rules in the help center, please edit the question.

(* some dummy data *)
test = RandomReal[{-1, 1}, {5, 5}];

(* do your thing *)
stuff = Block[{t = Transpose@test, mins},
mins = Min /@ t;
Transpose[{SparseArray[Unitize[t - mins], Automatic, 1]["NonzeroPositions"], mins}]];

(* show results *)
Column[{test // MatrixForm, stuff // MatrixForm}, Left, 2]


• While this is a correct answer this is obviously way over the head of the poster who is seemingly not even aware of the existence of Table. With all due respect of course, I don't think this answer will help him very much. – Sjoerd C. de Vries Sep 20 '15 at 10:13
• @ Sjoerd C. de Vries +1 but I also upvoted ciao also. Posting here means posting to all the members not just the OP. His answer did help me as did yours. So if you have a more advanced solution just let it rip and post it. – bobbym Sep 20 '15 at 18:32

There are many ways to do this in Mathematica and you can use many programming styles. Mathematica shines when using the functional programming style, but my guess is that's a bit early for you. So the answer below will refrain from most advanced stuff.

First, we'll need some function that fills your table, depending on the column and row values. Since you didn't specify anything here's a random one. You can use any function of your own instead.

f[row_, col_] := row col - row^2 col^3


Now, let's fill a table with data:

data = Table[f[row, col], {row, -1, 1, 0.0005}, {col, -3, 3}];


Finally, find the minimum values of all the columns (explanation follows):

Table[
min = Min[data[[All, c]]];
{Position[data[[All, c]], min, 1][[1, 1]], c},
{c, 1, Length[data[[1]]]}
]
(* {{2112, 1}, {2251, 2}, {3001, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}} *)


Explanation:

We loop over all columns, using loop variable c. Within the Table the first step is:

 min = Min[data[[All, c]]];


data[[All, c]] means "all rows of column c". This is a one-dimensional list. Min finds the minimum value of this list.

{Position[data[[All, c]], min, 1][[1, 1]], c},


Next, we use Position to find this minimum value and get the position. The argument 1 means we're interested in the first one. Since Position returns a list of positions we've to pick one, the first coordinate of the list in this case, and from this the first number. [[1,1]] accomplishes this. Together with {... ,c} we now have a {r, c} pair that will end up in the resulting list of the Table (because they are the last of the set of functions in the Table function).