2.61 How to apply a function to each value in a matrix?

Apply function \(f(x,n)\) to ragged matrix where \(n\) is value taken from the matrix and \(x\) is some other value defined somewhere else. The result of the function should replace the same position in the matrix where \(n\) was.

For example, given matrix \[ mat = \left ({\begin {array}[c]{ccc}1 & 2 & 3\\ 4 & 5 & \\ 7 & 8 & 9 \end {array}}\right ) \]

generate the matrix

\[ \left ({\begin {array} [c]{ccc}f[x,1] & f[x,2] & f[x,3]\\ f[x,4] & f[x,5] & \\ f[x,7] & f[x,8] & f[x,9] \end {array}}\right ) \]

Mathematica

The trick is to use Map with 2 at end.

ClearAll[f, x] 
mat = {{1, 2, 3}, {4, 5}, {7, 8, 9}}; 
Map[f[x, #] &, mat, {2}]
 

\( \{\{f(x,1),f(x,2),f(x,3)\},\{f(x,4),f(x,5)\},\{f(x,7),f(x,8),f(x,9)\}\} \)

 

Maple

#use double map 
restart; 
mat:=[[1,2,3],[4,5],[7,8,9]]; 
map(x->map(y->f(y),x),mat)
 

[[f(1), f(2), f(3)], [f(4), f(5)], [f(7), f(8), f(9)]]