7
$\begingroup$

I have the following data and want to replace only the first part in all sublists by the square root. It can be done by:

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};
t2 = Transpose[{Sqrt /@ data[[All, 1]]}];
t3 = Table[Flatten[AppendTo[t2[[i]], data[[i, 2 ;; 3]]]], {i, 3}]

and I get

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

What is a shorter way using Replace or other methods (and/or using patterns)?

$\endgroup$
4
  • 3
    $\begingroup$ This data /. {a_Integer, b_Integer, c_Integer} :> {Sqrt[a], b, c} seems to satisfy your immediate requirement. Is that the kind of answer you want ? $\endgroup$ Mar 30, 2020 at 16:15
  • 2
    $\begingroup$ {Sqrt[#[[1]]], #[[2]], #[[3]]} & /@ data or {Sqrt[#[[1]]], Sequence @@ Rest@##} & /@ data or ReplacePart[#, 1 -> Sqrt[#[[1]]]] & /@ data $\endgroup$
    – Bob Hanlon
    Mar 30, 2020 at 16:32
  • 2
    $\begingroup$ data[[;; , 1]] = Sqrt[data[[;; , 1]]] $\endgroup$
    – Fraccalo
    Mar 30, 2020 at 16:36
  • 2
    $\begingroup$ data[[All, 1]] = Sqrt[data[[All, 1]]] . $\endgroup$ Mar 30, 2020 at 16:44

6 Answers 6

13
$\begingroup$

Here are a few ways:

MapAt[Sqrt, data, {All, 1}]
Replace[data, {x_, y___} :> {Sqrt[x], y}, {1}]
data // Query[All, {1 -> Sqrt}]
SubsetMap[Sqrt, data, {All, 1}]
ReplacePart[data, {i_, 1} :> Sqrt[data[[i, 1]]]]
data2 = data;
data2[[All, 1]] = Sqrt[data2[[All, 1]]];
data2
$\endgroup$
1
  • 1
    $\begingroup$ Wow! That gives me material to breed over. Great! $\endgroup$
    – user57467
    Mar 30, 2020 at 16:52
9
$\begingroup$
☺ = {#^(1/2), ##2} &;

☺ @@@ data
{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

or

☺☺ = {#^(1/2), ##2} & @@@ # &;

☺☺ @ data
{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}
$\endgroup$
2
  • $\begingroup$ I can understand your first solution, but what does "@@@ # &" in the second part really mean? $\endgroup$
    – user57467
    Mar 31, 2020 at 4:21
  • $\begingroup$ @user57467, ` foo@@@#&` replaces heads at level 1 of input expression with foo (see Apply) $\endgroup$
    – kglr
    Mar 31, 2020 at 4:52
8
$\begingroup$

What kglr posted, but a bit more "golfed"

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

{√#, ##2} & @@@ data
{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}
$\endgroup$
1
  • 2
    $\begingroup$ I haver never seen this symbol. Looks good! :) $\endgroup$
    – user57467
    Mar 31, 2020 at 4:25
2
$\begingroup$
list = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

Showing some of the newer functions

SubsetMap[Sqrt, {1}] /@ list

ReplaceAt[x_ :> Sqrt[x], {All, 1}] @ list

SequenceReplace[{{x_, y__}} :> {Sqrt[x], y}] @ list

All return

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}
$\endgroup$
2
$\begingroup$
data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

Using Distribute:

Distribute[data, Function[n, {Sqrt@n[[1]], Splice@n[[2 ;;]]}] /@ # &]

Result:

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

$\endgroup$
2
$\begingroup$

Sjoerd Smit has posted a very neat method for applying a function to a matrix column:

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

Query[All,{1->(Sqrt[#]&)}]@data

(* {{7,35,14},{8,40,16},{9,45,18}} *)

In this instance, Query is using MapAt 'under the hood':

Query[All,{1->(Sqrt[#]&)}]//Normal

(* MapAt[Sqrt[#1]&,{All,1}] *)
$\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.