7
$\begingroup$

Having a table given by, let's say

data = {a,b,c,d},{e,f,g,h}

how can I update data in a simple way

data = Something[data]

to display

data = {a,b,c,d, operation[a,b,c,d]},{e,f,g,h, operation[e,f,g,h]}

for a given function "operation"?

Specifically, I'm finding solutions for an expression function == 1 using a RandomSearch, but I'd like to display also the resulting value that is close to target in each search:

target = 1

solutionCandidates =
  Table[NMinimize[{Abs[(function[var1, var2, var3] -
         target)^2], var1min < var1 < var1max, var2min < var2 < var2max, var3min < var3 < var3max}, {var1, var2, var3},
    Method -> {"RandomSearch", "RandomSeed" -> i,
      "SearchPoints" -> 10}], {i, 10}];

solutions = Select[solutionCandidates, #[[1]] < 10^-10 &][[All, 2]];
$\endgroup$

7 Answers 7

10
$\begingroup$
data = {{a, b, c, d}, {e, f, g, h}};

{##, operation @ ##} & @@@ data
{{a, b, c, d, operation[a, b, c, d]}, 
 {e, f, g, h, operation[e, f, g, h]}}
$\endgroup$
5
$\begingroup$

Try Map !

As a simple example

func = # . # &
Map[Join[#, {func[#]}] &, {{a, b, c, d}, {e, f, g,h}}, 1]
(*{{a, b, c, d, a^2 + b^2 + c^2 + d^2}, {e, f, g,h,e^2 + f^2 + g^2 + h^2}}*)
$\endgroup$
4
$\begingroup$
data = {{a, b, c, d}, {e, f, g, h}}
f[k_List] := {Sequence @@ k, f @@ k}

Map this function on each data item or as required.

f /@ data

{{a, b, c, d, f[a, b, c, d]}, {e, f, g, h, f[e, f, g, h]}}

$\endgroup$
3
$\begingroup$
lst = {{a, b, c, d}, {e, f, g, h}}
Append[#, operation[#]] & /@ lst
$\endgroup$
3
$\begingroup$

Using Query

data = {{a, b, c, d}, {e, f, g, h}};

Query[All, {Apply[Sequence], Apply[operation]}] @ data

gives

{{a, b, c, d, operation[a, b, c, d]},
 {e, f, g, h, operation[e, f, g, h]}}
$\endgroup$
3
$\begingroup$
data = {{a, b, c, d}, {e, f, g, h}};

Using ComapApply (new in 14.0)

ComapApply[{Sequence, op}] /@ data

returns

{{a, b, c, d, op[a, b, c, d]}, 
{ e, f, g, h, op[e, f, g, h]}}
$\endgroup$
1
$\begingroup$

Using MapThread:

data = {{a, b, c, d}, {e, f, g, h}};

MapThread[Append, {#, operation @@@ #}] &@data

(*{{a, b, c, d, operation[a, b, c, d]}, {e, f, g, h, operation[e, f, g, h]}}*)

Also, using Flatten:

Flatten[{##, operation[##]}] & @@@ data

(*{{a, b, c, d, operation[a, b, c, d]}, {e, f, g, h, operation[e, f, g, h]}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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