-1

I need to make a Module whose input is a function of 2 variables and 2 variables m,n so i can make a Matrix with Array. Basicaly, I need this:

f[x_, y_] = Input[]
M = Array[f, {m,n}]
MatrixForm[M]

to make it work in a Module.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
Gorjan
  • 1
  • 1
  • Welcome to Mathematica.SE! I suggest that: 1) You 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. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey May 20 '15 at 15:12

1 Answers1

1

Would something like this work for you?

generatematrix :=
  Module[
   {rows, columns, function, result},
   rows = Input["Enter the number of rows"];
   columns = Input["Enter the number of column"];
   function = Input["Enter the function"];
   result = Array[function, {rows, columns}];
   MatrixForm@ result
  ]

Every time you evaluate generatematrix, three input dialogs will pop up, asking for the value of the first variable, the second variable, and then for the function you want to use. So for instance, if one entered 2, 3, and f in the three dialogs, the output would be:

Mathematica graphics

I'd like to point out something, though, regarding the fact that you wanted the MatrixForm of this expression returned. As you may be aware, MatrixForm is an output wrapper that may get in the way of further calculations carried out on your output matrix. It may be safer to return a plain output format (i.e. list of lists), and do the formatting outside of the function:

generatematrixNoMatrixForm :=
  Module[
   {rows, columns, function, result},
   rows = Input["Enter the number of rows"];
   columns = Input["Enter the number of column"];
   function = Input["Enter the function"];
   Array[function, {rows, columns}]
  ]

MatrixForm@ generatematrixNoMatrixForm
MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • I don't quite understand why, but after returning the output I get another dialog asking me for the number of rows again. – LLlAMnYP May 20 '15 at 22:34
  • @LLlAMnYP Hmm, weird. I don't see that on my end (v.10.1 Win7-64). I tried with either version, the one with MatrixForm, and the one without. – MarcoB May 20 '15 at 23:20