0

I have a function g[x,r] yielding a 3x1 vector, where x is a 9x1 vector and r is a 3x1 vector.

Now I define multiple r by:

R = Array[Subscript[r, ##] &, {3, N}]

Now I want to construct

G[x_,R_] := {g[x,r_1],g[x,r_2],g[x,r_3],...,g[x,r_N]}

Where r_i is the ith column vector of R, and G should be a 3Nx1 vector. I reckon I would have to use Map or Outer, but cannot see how.

crlb
  • 101
  • 1
  • First, notice that the character "_" in Mathematica is the function Blank which is a pattern object that can stand for any Wolfram Language expression. – rhermans Apr 26 '17 at 16:29
  • You should avoid using Subscript while defining symbols (variables). Subscript[x, 1] is not a symbol, but a compound expression where Subscript is an operator without built-in meaning. You expect to do $x_1=2$ but you are actually doing Set[Subscript[x, 1], 2] which is to assign a Downvalue to the oprator Subscript and not an Ownvalue to an indexed x as you may intend. Read how to properly define indexed variables here – rhermans Apr 26 '17 at 16:30
  • With respect to "map a function over a dimension", probably you should look at MapAt – rhermans Apr 26 '17 at 16:34

1 Answers1

2

Switch your indices on the array command so you have 3x1 row vectors

    rr = Array[f[##] &, {10, 3}];
    rr // MatrixForm

$$ \left( \begin{array}{ccc} f(1,1) & f(1,2) & f(1,3) \\ f(2,1) & f(2,2) & f(2,3) \\ f(3,1) & f(3,2) & f(3,3) \\ f(4,1) & f(4,2) & f(4,3) \\ f(5,1) & f(5,2) & f(5,3) \\ f(6,1) & f(6,2) & f(6,3) \\ f(7,1) & f(7,2) & f(7,3) \\ f(8,1) & f(8,2) & f(8,3) \\ f(9,1) & f(9,2) & f(9,3) \\ f(10,1) & f(10,2) & f(10,3) \\ \end{array} \right) $$

Then just run Map[ ] over it and Flatten[ ] the results

    g[x, y_] := {1, 2, 3}
    Map[g[x, #] &, rr] // Flatten

$$\{1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3\}$$

MikeY
  • 7,153
  • 18
  • 27
  • You can also use g[x, #] & /@ Transpose[R] with OP's definition of R. Transpose does the flipping. – C. E. Apr 26 '17 at 17:31