2
$\begingroup$

So I am trying to print out a table using the map function. My code looks like this:

parameters = Table[{a, z}, {a, 1.251,2.5,5.251}, {z, {1, 10, 15, 25, 30}}];

Map[r+a+z &, parameters, {2}]

Unfortunately, this gets me a list with the parameters unchanged. What I want is for this function to return a list of the sort {r+1.251+1},{r+1.251+10},{r+1.251+15}...{r+2.5+1},{r+2.5+10},{r+2.5+15}... etc etc

Obviously in the final output, the numbers would actually be added up.

$\endgroup$
  • $\begingroup$ Map[({r + #[[1]] + #[[2]]}) &, Flatten[parameters, 1]] $\endgroup$ – RMMA Oct 11 '17 at 13:10
  • $\begingroup$ or Map[Plus[r, Total@#] &, parameters, {2}] $\endgroup$ – kglr Oct 11 '17 at 13:14
  • $\begingroup$ @RMMA! Cool that works. What do the hashtags actually do here? $\endgroup$ – Dio Oct 11 '17 at 13:14
  • $\begingroup$ Map[r + Total[#] &, First@parameters] The hash mark stands for the argument; the ampersand is postfix for Function. See the documentation for Function. $\endgroup$ – Alan Oct 11 '17 at 13:19
  • $\begingroup$ I'm not sure if I should VTC as a simple mistake. Note that a and z are not stored or remembered in any way as references to anything in parameters. Another neat way to solve your problem would be Total[parameters, {3}] + r. Further reading is already covered in other comments $\endgroup$ – LLlAMnYP Oct 11 '17 at 14:25
1
$\begingroup$
Join @@ Map[{r + Total@#} &, parameters, {2}]
Join @@ Map[{r + Plus @@ #} &, parameters, {2}]
Join @@ Map[{Total[{r, #}, 2]} &, parameters, {2}]

all give

{{2.251 + r}, {11.251 + r}, {16.251 + r}, {26.251 + r}, {31.251 + r}, {3.5 + r}, {12.5 + r}, {17.5 + r}, {27.5 + r}, {32.5 + r}, {6.251 + r}, {15.251 + r}, {20.251 + r}, {30.251 + r}, {35.251 + r}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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