Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Let x and y be arrays defined as Array[x, m], Array[y,n] respectively.

How to generate the following function of $x$ and $y$ for variable $k$:

x[1]<y[n-k+1] && x[2]<y[n-k+2] && ... && x[k]<y[n]

Assume that $k<n$ and $k<m$.

share|improve this question
    
Related/possible dupe: mathematica.stackexchange.com/q/10211/5 –  rm -rf Apr 16 '13 at 13:40

3 Answers 3

up vote 4 down vote accepted

Another approach, using Table:

n = 15;

doIt[k_ /; k < n] := And @@ Table[x[i] < y[n - k + i], {i, 1, k}]

Then:

doIt[5]
x[1] < y[11] && x[2] < y[12] && x[3] < y[13] && x[4] < y[14] && x[5] < y[15]

Note: maybe you want to condition k on m as well

share|improve this answer

This can be done in many ways. A good approach uses e.g. MapThread, taking e.g. $n = 7, m = 6$ :

And @@ MapThread[ Less, {Array[y, 7][[3 ;;]], Reverse[Array[x, 6]][[2 ;;]]}] //
TraditionalForm
y(3) < x(5) && y(4) < x(4) && y(5) < x(3) && y(6) < x(2) && y(7) < x(1)

or Inner :

Inner[ Less, Array[y, 7][[3 ;;]], Reverse[Array[x, 6]][[2 ;;]], And]

Edit

As suggested in the comments let's add another general solution:

sol[k_, m_, n_] /; 1 <= k <= m <= n := 
    Inner[ Less, Take[ Array[y, n], {k, m}], 
                 Take[ Reverse @ Array[x, n], {k - 1, m - 1}], And]

so we have, e.g.

sol[4, 6, 7]
y[4] < x[5] && y[5] < x[4] && y[6] < x[3]
share|improve this answer
    
I prefer the Inner[] version myself, though I personally prefer Take[] to Part[]+Span[]. –  J. M. Apr 16 '13 at 13:10
    
@J.M. With Inner there is no need to use Apply as in the case of MapThread and it seems to be faster. –  Artes Apr 16 '13 at 13:19
    
Right, and the problem's structure just screams for Inner[] to be used. :) –  J. M. Apr 16 '13 at 13:34

If

x = Symbol["x" <> ToString[#]] & /@ Range[10];
y = Symbol["y" <> ToString[#]] & /@ Range[6];
k=4;

And @@ Thread[x[[1 ;; k]] < y[[-k ;;]]]
(* x1 < y3 && x2 < y4 && x3 < y5 && x4 < y6 *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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