3
$\begingroup$

This question already has an answer here:

Say I have table in the shape of a matrix of the form:

f[x_] = {{1, 1/4 + x/4}, {0, 1/4 + x/4}}

and a list of numbers

list = {1,2,3,4,5}  

What is the easiest way to get a matrix of the same shape as above with its elements corresponding to the each function element of f[x_] applied to the list?

The trouble here is that f[x_] involves constant functions so that doing f[list] gives:

In[188]:= f[list]
Out[188]= {{1,{1/2,3/4,1,5/4,3/2}},{0,{1/2,3/4,1,5/4,3/2}}}

(only the non constant elements are evaluated).

On the other hand, doing, f /@ list evaluates f[x_] as a matrix valued function and gives a list of 2 by 2 matrices:

 In[189]:= f/@list
 Out[189]= {{{1,1/2},{0,1/2}},{{1,3/4},{0,3/4}},{{1,1},{0,1}},{{1,5/4},{0,5/4}},{{1,3/2},{0,3/2}}}

This is not what I want either.

What I want is

{{{1,1,1,1,1},{1/2,3/4,1,5/4,3/2}},{{0,0,0,0,0},{1/2,3/4,1,5/4,3/2}}}
$\endgroup$

marked as duplicate by Mr.Wizard Jan 19 '17 at 18:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ good point. Just edited. $\endgroup$ – jrekier Jan 19 '17 at 11:44
  • $\begingroup$ {{1+0 x, 1/4 + x/4}, {0 x, 1/4 + x/4}}? $\endgroup$ – Kuba Jan 19 '17 at 11:47
  • $\begingroup$ @Kuba Only if you use SetDelayed $\endgroup$ – Simon Rochester Jan 19 '17 at 11:48
  • $\begingroup$ After a review of the answers to (129755) I have concluded that this question "already has an answer" there, and I have marked it accordingly. My favorite solution is bienti's: f[x_] := {{1 + 0 x, 1/4 + x/4}, {0 x, 1/4 + x/4}} $\endgroup$ – Mr.Wizard Jan 19 '17 at 18:37
  • $\begingroup$ I had seen and tested the mentioned other solutions before asking my question. These are not applicable here as these only work for one dimensional arrays. $\endgroup$ – jrekier Jan 20 '17 at 11:25
2
$\begingroup$
f[x_] = {{1, 1/4 + x/4}, {0, 1/4 + x/4}};
list = {1, 2, 3, 4, 5};

Then

Transpose /@ Transpose[f /@ list, {2, 1}]

{{{1, 1, 1, 1, 1}, {1/2, 3/4, 1, 5/4, 3/2}}, {{0, 0, 0, 0, 0}, {1/2, 3/4, 1, 5/4, 3/2}}}

$\endgroup$
  • $\begingroup$ Awesome! Thanks. $\endgroup$ – jrekier Jan 19 '17 at 11:50

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