I am trying to find the minimum value of a row of a matrix and its corresponding column. Please run the code below. OptV is the matrix that I would like to create which must give the output of the minimum of each row and its corresponding column index in VAll matrix.
Clear[OptV, V,VAll, NU, EDD, PCP]
binc = 0.2;
InitV[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] = Table[1, {b, 0, 1, binc}];
V[1] := InitV[cs, cc, ED, P, NL, pR, pW, b];
NU[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] = Table[NL + (1 - b) (cs + (1 - pR) (P + pW ED)) + b ED, {b, 0, 1,
binc}];
PCP[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] = Table[(1 - b) cs +
b cc + (1 - (1 - b) pR) (P + (b + (1 - b) pW) ED), {b, 0, 1,
binc}];
EDD[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] = Table[ED, {b, 0, 1, binc}];
For[i = 2, i < 20, i++, V[i_Integer] := V[i] = Min /@ Transpose[{Table[(1 - b) cs + b cc + (1 - (1 - b) pR), {b, 0, 1,
binc}]*V[i - 1], PCP[cs, cc, ED, P, NL, pR, pW, b],
NU[cs, cc, ED, P, NL, pR, pW, b],
EDD[cs, cc, ED, P, NL, pR, pW, b]}]];
W[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] = Table[(1 - b) cs + b cc + (1 - (1 - b) pR), {b, 0, 1, binc}]*V[i];
VAll[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] =
Transpose[{W[cs, cc, ED, P, NL, pR, pW, b],
PCP[cs, cc, ED, P, NL, pR, pW, b],
EDD[cs, cc, ED, P, NL, pR, pW, b],
NU[cs, cc, ED, P, NL, pR, pW, b]}];
OptV[cs_, cc_, ED_, P_, NL_, pR_, pW_, b_] =
With[{m = Min@#}, {m, Position[#, m][[1, 1]]}] & /@
VAll[cs, cc, ED, P, NL, pR, pW, b];
With[{cs = 1, cc = 10, ED = 100, P = 25, NL = 5, pR = 0.1, pW = 0.25},
MatrixForm[VAll[cs, cc, ED, P, NL, pR, pW, b]]]
With[{cs = 1, cc = 10, ED = 100, P = 25, NL = 5, pR = 0.1, pW = 0.25},
MatrixForm[OptV[cs, cc, ED, P, NL, pR, pW, b]]]
ai
in the first two functions was meant as multiplication? $\endgroup$ – ciao May 1 '16 at 1:04With[{m = Min@#}, {m, Position[#, m][[1, 1]]}] & /@ Opt[...]
if only 1 minimum is expected per row,With[{m = Min@#}, {m, Position[#, m]}] & /@ Opt[...]
if multiple minimums can be in a row, in which case the vector of positions results. If the array is going to be huge, there are more efficient ways... $\endgroup$ – ciao May 1 '16 at 1:10