I'm new to use the amazing map function in Mathematica, and I've found it really elegant to program in simple lines of code. However, I'd like to perform more elegant stuff. I've found this question, but its not what I'm trying to achieve. How do I use Map for a function with two arguments?
I want to apply a two, or more, argument function using columns of a list as arguments
My approach is the following:
For an arbitrary list:
vector = Table[ {RandomReal[], RandomReal[], RandomReal[]}, {n, 0, 10} ]
I want to apply the Tan[] using two columns as an arguments, so I tried unsuccessfully writing:
Tan[#1/#2] & {vector[[;; , {1, 2}]]}
I know I can use Table[] (in a for loop way) like this to achieve my solution:
Table[{ Tan[vector[[n, 2]]/vector[[n, 1]]]}, {n, 1, Length[vector]}]
However, I'd like to master the functional programming style. How can I achieve the same output using Map(/@)
Any tutorial/book/lecture would be greatly appreciated as well.
Thanks!
Tan[#1/#2] & @@@ vector[[;; , {1, 2}]]
would do it; see manual:@@@
means "apply at level 1". Alternatively,Tan[#[[1]]/#[[2]]] & /@ vector
. Maybe invert#[[1]]
and#[[2]]
– I'm unsure as to which one you want. $\endgroup$Tan[vector[[All,2]]/vector[[All,1]]]
$\endgroup$