I have to define a function of two variables that yields a high dimensional matrix, with each entry containing a scalar function, that uses its coordinates in the matrix as input. like this:

f[x_,a_] := {{g[1,1][x]*a, g[1,2][x]*a}, {g[2,1][x]*a, g[2,2][x]*a}}

Is there any way I can assign the elements in a smart way without mapping the whole matrix by hand?

EDIT: I've been asked for an example of g. This is taken out of the original code:

g[n_, m_] := Table[Chop[
    I*Conjugate[h[[n]][[i]]].(hx[[m]][[i]] - h[[m]][[i]])/
      step, 3*10^-3],
   {i, 1, Length[qvals]}];
  • $\begingroup$ Use Array or Table. $\endgroup$
    – Szabolcs
    Jul 22, 2015 at 15:03
  • $\begingroup$ Do you have an example of what g is here? $\endgroup$ Jul 22, 2015 at 15:05
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 22, 2015 at 15:07
  • $\begingroup$ That's not a good example. It depends on many undefined free variables. $\endgroup$
    – m_goldberg
    Jul 22, 2015 at 16:27
  • $\begingroup$ f[x_, a_] = a Array[g[##][x] &, {2, 2}] $\endgroup$ Jul 22, 2015 at 16:43

1 Answer 1

gs = Flatten @ Table[With[{m = m, n = n}, Sin[m #/n^2] &], {m, 2}, {n, 2}];
{Sin[#1/1^2] & , Sin[#1/2^2] & , Sin[(2*#1)/1^2] & , Sin[(2*#1)/2^2] &}
f[x_, a_] = a Through[gs[x]];
f[u, b]
{b*Sin[u], b*Sin[u/4], b*Sin[2*u], b*Sin[u/2]}

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.