1
$\begingroup$

I have a given list of some numbers e.g.

a = {0, 0.233, 0.65, 1.56};

I want to evaluate a function

Func[x_?NumberQ,y_?NumberQ,z_?NumberQ] 

where one variable z takes its values from the specified list a.

How do I do this in best way ?

$\endgroup$
  • $\begingroup$ The best way depends on the function, its intended use, and other goals. $\endgroup$ – Michael E2 Aug 2 '18 at 18:57
4
$\begingroup$

Data

a = Range[5]
(* {1, 2, 3, 4, 5} *)

Many options

Listable

SetAttributes[func, Listable];
func[x_?NumberQ, y_?NumberQ, z_?NumberQ] = f[x, y, z];
func[x, y, a]
(* {func[x, y, 1], func[x, y, 2], func[x, y, 3], func[x, y, 4], func[x, y, 5]} *)

Map

f[x, y, #] & /@ a
(* {f[x, y, 1], f[x, y, 2], f[x, y, 3], f[x, y, 4], f[x, y, 5]} *)

Thread

Thread[f[x, y, a]]
(* {f[x, y, 1], f[x, y, 2], f[x, y, 3], f[x, y, 4], f[x, y, 5]} *)

Table

Table[
 f[x, y, z]
 , {z, a}
 ]
(* {f[x, y, 1], f[x, y, 2], f[x, y, 3], f[x, y, 4], f[x, y, 5]} *)
$\endgroup$
2
$\begingroup$

If you have fixed, known values x1 and y1 for the first variables, then how about the following?

a={0, 0.233, 0.65, 1.56};
Func[x1,y1,#]&/@a
$\endgroup$
  • $\begingroup$ It worked. Thanks !! $\endgroup$ – Prag Aug 3 '18 at 10:42
0
$\begingroup$

Define your function with just $x$ and in the body refer to x[[7]] (or multiple entries, or whatever components of $x$ are used as $z$).

$\endgroup$
  • $\begingroup$ Thank you !! I need the argument i of x[[i]] to run from 1... length[A]. So I need to create a table in the end which implements the following: B = Table[Func[x0,y0,z] : z goes from A[1] to A[N] ]. $\endgroup$ – Prag Aug 2 '18 at 17:43

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.