1
$\begingroup$

If I have two variables x and y and for every value of x how I can repeat y values as what I did in ( xytab ) manually.

    x = Table[0.2712 - n (0.0003), {n, 0, 2}]
{0.2712, 0.2709, 0.2706}

y = Table[0.07784 - n (0.0003), {n, 0, 2}]
{0.07784, 0.07754, 0.07724}

xytab = {{0.2712`, 0.07784`}, {0.2709`, 0.07784`}, {0.2706`, 
0.07784`}, {0.2712`, 0.07754000000000001`}, {0.2706`, 
0.07754000000000001`}, {0.2709`, 0.07754000000000001`}, {0.2712`, 
0.07724`}, {0.2709`, 0.07724`}, {0.2706`, 0.07724`}};

Below I generated a table including the points(f)that are the result from another code:

       f = {-12.614324138097164`, -11.403557753606028`, 
  -11.972267934125227`, -13.95736964188886`, -15.160073023551389`, 
  -11.37937735952056`, -11.945532093596869`, -13.965637862424334`, 
  -16.013160208902804`}

    x1 = xytab[[All, 1]]
y1 = xytab[[All, 2]]

If my table is as below how I can find the minimum of the third column that means(f) for every value of y automatically

    tab = Table[{x1[[i]], y1[[i]], f[[i]]}, {i, 1, Length[x1]}];

{{0.2712, 0.07784, -12.6143}, {0.2709, 0.07784, -11.4036}, {0.2706, 
 0.07784, -11.9723}, {0.2712, 0.07754, -13.9574}, {0.2706, 
 0.07754, -15.1601}, {0.2709, 0.07754, -11.3794}, {0.2712, 
 0.07724, -11.9455}, {0.2709, 0.07724, -13.9656}, {0.2706, 
 0.07724, -16.0132}}

So, I need two things, first how to repeat value of y for every value of x and then how to find the minimum for every y automatically.

Thanks.

$\endgroup$

3 Answers 3

1
$\begingroup$
x = Table[0.2712 - n (0.0003), {n, 0, 2}];
y = Table[0.07784 - n (0.0003), {n, 0, 2}];

xytab = Outer[{#2, #1} &, y, x] // Flatten[#, 1] &

(* {{0.2712, 0.07784}, {0.2709, 0.07784}, {0.2706, 0.07784}, {0.2712, 
  0.07754}, {0.2709, 0.07754}, {0.2706, 0.07754}, {0.2712, 
  0.07724}, {0.2709, 0.07724}, {0.2706, 0.07724}} *)

f = {-12.614324138097164`, -11.403557753606028`, \
-11.972267934125227`, -13.95736964188886`, -15.160073023551389`, \
-11.37937735952056`, -11.945532093596869`, -13.965637862424334`, \
-16.013160208902804`};

x1 = xytab[[All, 1]];
y1 = xytab[[All, 2]];

tab = Transpose[{x1, y1, f}]

(* {{0.2712, 0.07784, -12.6143}, {0.2709, 0.07784, -11.4036}, {0.2706, 
  0.07784, -11.9723}, {0.2712, 0.07754, -13.9574}, {0.2709, 
  0.07754, -15.1601}, {0.2706, 0.07754, -11.3794}, {0.2712, 
  0.07724, -11.9455}, {0.2709, 0.07724, -13.9656}, {0.2706, 
  0.07724, -16.0132}} *)

MinimalBy[#, #[[3]] &] & /@ GatherBy[tab, #[[2]] &]

(* {{{0.2712, 0.07784, -12.6143}}, {{0.2709, 
   0.07754, -15.1601}}, {{0.2706, 0.07724, -16.0132}}} *)
$\endgroup$
1
  • $\begingroup$ Thank you very much! $\endgroup$
    – Ghady
    Feb 24, 2018 at 17:01
2
$\begingroup$

Maybe this helps:

x = Table[0.2712 - n (0.0003), {n, 0, 2}];
y = Table[0.07784 - n (0.0003), {n, 0, 2}];
xytab = Flatten[Transpose[Outer[List, x, y, 1]], 1];
f = {-12.614324138097164`, -11.403557753606028`, \
-11.972267934125227`, -13.95736964188886`, -15.160073023551389`, \
-11.37937735952056`, -11.945532093596869`, -13.965637862424334`, \
-16.013160208902804`};
tab = Join[xytab, Partition[f, 1], 2]
Min[tab[[All, 3]]]

A minimal position can be found with

i=Ordering[tab[[All, 3]], 1][[1]];
tab[[i]]

{0.2706, 0.07724, -16.0132}

For getting the minimum for each fixed value of y and with data layout, you may use

fpartitioned = Partition[f, Length[x]];
Min /@ fpartitioned

and

i = Ordering[#, 1][[1]] & /@ fpartitioned
$\endgroup$
1
  • $\begingroup$ Thank you so much for your response. But If I need the minimum for every value of y that means there are 3 minima, how to find them? $\endgroup$
    – Ghady
    Feb 24, 2018 at 15:39
2
$\begingroup$

Maybe this will help for you.

tab = 
  {{0.2712,0.07784,-12.6143}, {0.2709,0.07784,-11.4036}, {0.2706,0.07784,-11.9723}, 
   {0.2712,0.07754,-13.9574}, {0.2706,0.07754,-15.1601}, {0.2709,0.07754,-11.3794}, 
   {0.2712,0.07724,-11.9455}, {0.2709,0.07724,-13.9656}, {0.2706,0.07724,-16.0132}};

MinimalBy[#[[3]] &] /@ SplitBy[tab, #[[2]] &]
{{{0.2712,0.07784,-12.6143}}, 
  {{0.2706,0.07754,-15.1601}}, 
  {{0.2706,0.07724,-16.0132}}}
$\endgroup$
4
  • $\begingroup$ Thank you so much! $\endgroup$
    – Ghady
    Feb 24, 2018 at 17:00
  • $\begingroup$ @Ghady. If you find this helpful, why do you not up-vote it? $\endgroup$
    – m_goldberg
    Feb 24, 2018 at 17:12
  • $\begingroup$ Sorry, how to vote? $\endgroup$
    – Ghady
    Feb 24, 2018 at 18:00
  • $\begingroup$ To up-vote an answer, click on the up pointing triangle to the left of the answer. You now have enough reputation points to up-vote as many answers to your as like -- any answer you find helpful (you are not limited to just one vote). $\endgroup$
    – m_goldberg
    Feb 24, 2018 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.