2
$\begingroup$

I want to generate a table in which two of the variables (x,y) are fixed with 1<->1 correspondence while the third variable is varying in the given range for each each sublist. I want to program for this in the input, NOT by generating the general output and then removing the unnecessary part. Example is given here:

fnn[x_, y_, z_] := x*Sin[y]*Cos[z]
xr = {1, 2, 3, 4, 5};
yr = {6, 7, 8, 9, 10};
zr = {11, 12, 13, 14, 15};

I want the output in the form,

{{fnn[1, 6, z]},{fnn[2, 7, z]},{fnn[3, 8, z]},{fnn[4, 9, z]},{fnn[5, 10, z]}},

where z varies in the range, {11, 12, 13, 14, 15}. I do not need e.g. fnn[1, 7, z], fnn[2, 8, z] etc.

$\endgroup$

4 Answers 4

5
$\begingroup$
Clear["Global`*"]

fnn[x_, y_, z_] := x*Sin[y]*Cos[z]
xr = {1, 2, 3, 4, 5};
yr = {6, 7, 8, 9, 10};
zr = {11, 12, 13, 14, 15};

MapThread[
 Thread[Inactive[fnn][#1, #2, zr]] &,
 {xr, yr}
 ]

or

Inner[Thread[Inactive[fnn][#1, #2, zr]] &, xr, yr, List]

Result

enter image description here

$\endgroup$
3
  • $\begingroup$ Why are the fnn[x,y,z] not evaluated? The output should be like 1*Sin[6]*Cos[11],....... etc? $\endgroup$
    – SciJewel
    Sep 29, 2023 at 16:21
  • $\begingroup$ I got it. Coz you have added "Inactive" before "fnn". Thanks! $\endgroup$
    – SciJewel
    Sep 29, 2023 at 16:23
  • $\begingroup$ If these evaluate, then the larger picture would be lost. You can always remove the Inactive when you are satisfied with the output. $\endgroup$
    – Syed
    Sep 29, 2023 at 16:34
2
$\begingroup$

You can also use the second and third arguments of Thread combined with MapThread as follows:

Map[Thread] @ Thread[FOO[xr, yr, zr], List, 2]

enter image description here

Map[Thread] @ Thread[FOO[xr, yr, zr], List, 2] /. FOO -> fnn

enter image description here

Alternatively, use

ReleaseHold @ Map[Thread] @ Thread[Hold[fnn][xr, yr, zr], List, 2]

to get the same result.

$\endgroup$
1
$\begingroup$

Since all components of fnn (Times, Sin, Cos) are Listable, you can simply map fnn[xr, yr, #] & on zr:

Map[fnn[xr, yr, #] &] @ zr 

enter image description here

$\endgroup$
1
$\begingroup$
Clear[f]
Table[f[xr[[n]], yr[[n]], j], {n, Length[xr]}, {j, zr}]

{{f[1, 6, 11], f[1, 6, 12], f[1, 6, 13], f[1, 6, 14], f[1, 6, 15]}, {f[2, 7, 11], f[2, 7, 12], f[2, 7, 13], f[2, 7, 14], f[2, 7, 15]}, {f[3, 8, 11], f[3, 8, 12], f[3, 8, 13], f[3, 8, 14], f[3, 8, 15]}, {f[4, 9, 11], f[4, 9, 12], f[4, 9, 13], f[4, 9, 14], f[4, 9, 15]}, {f[5, 10, 11], f[5, 10, 12], f[5, 10, 13], f[5, 10, 14], f[5, 10, 15]}}

%/.f->fnn

{{Cos[11] Sin[6], Cos[12] Sin[6], Cos[13] Sin[6], Cos[14] Sin[6], Cos[15] Sin[6]}, {2 Cos[11] Sin[7], 2 Cos[12] Sin[7], 2 Cos[13] Sin[7], 2 Cos[14] Sin[7], 2 Cos[15] Sin[7]}, {3 Cos[11] Sin[8], 3 Cos[12] Sin[8], 3 Cos[13] Sin[8], 3 Cos[14] Sin[8], 3 Cos[15] Sin[8]}, {4 Cos[11] Sin[9], 4 Cos[12] Sin[9], 4 Cos[13] Sin[9], 4 Cos[14] Sin[9], 4 Cos[15] Sin[9]}, {5 Cos[11] Sin[10], 5 Cos[12] Sin[10], 5 Cos[13] Sin[10], 5 Cos[14] Sin[10], 5 Cos[15] Sin[10]}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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