6
$\begingroup$

I have the following list containing 2-element sublists:

list={{0.01, 0.037348}, {0.03, 0.165434}, {0.1, 0.263921}, {0.3, 
  0.560191}, {1., 0.968857}, {3., 1.50965}, {10., 2.36502}, {30., 
  3.07659}, {100., 3.73412}, {300., 4.4931}, {1000., 5.06818}, {3000.,
   5.65423}, {10000., 6.00944}}

How can I get the natural logarithm (Log[]) of the first element of each sublist (e.g. 0.01, 0.03.....10000) only while leaving the second one intact?. I would like this operation to apply to other sublists of different lengths.

$\endgroup$
5
  • 3
    $\begingroup$ {Log[First[#]],Last[#]}&/@list will make a new list with log of the first element. $\endgroup$
    – flinty
    Jun 28, 2020 at 1:26
  • $\begingroup$ This works great! @flinty. Thank you very much !!! $\endgroup$
    – John
    Jun 28, 2020 at 1:29
  • 2
    $\begingroup$ Or {Log@#1, #2} & @@@ list. There are many similar questions and this will be signed as a duplicate. $\endgroup$
    – Artes
    Jun 28, 2020 at 1:50
  • 1
    $\begingroup$ list[[All,1]]=Log@list[[All,1]] $\endgroup$ Jun 28, 2020 at 2:33
  • $\begingroup$ If you're ultimately planning to plot this data, you may want to look at the documentation for ListLogPlot, ListLogLinearPlot, and/or ListLogLogPlot. $\endgroup$ Jun 28, 2020 at 14:04

6 Answers 6

6
$\begingroup$

This should be fast for large lists:

Transpose[{Log @ #, #2} & @@ Transpose[#]] & @ list
{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, 
{0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517,  3.73412},
{5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}
$\endgroup$
10
$\begingroup$

You may use MapAt.

MapAt[Log, {All, 1}]@list
{{-4.60517, 0.037348}, {-3.50656, 0.165434}, 
 {-2.30259, 0.263921}, {-1.20397, 0.560191}, 
 {0., 0.968857}, {1.09861, 1.50965}, 
 {2.30259, 2.36502}, {3.4012, 3.07659}, 
 {4.60517, 3.73412}, {5.70378, 4.4931}, 
 {6.90776, 5.06818}, {8.00637, 5.65423}, 
 {9.21034, 6.00944}}

Hope this helps.

$\endgroup$
7
$\begingroup$

In addition, in v 12.0 and greater, you may use SubsetMap

SubsetMap[Log, list, {All,1}]

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

$\endgroup$
3
$\begingroup$
newlist=Table[{Log[list[[k]]],list[[k]]},{k,1,Length[list]}]

is inelegant but will work.

$\endgroup$
2
$\begingroup$
Query[All, {1 -> Log}] @ list

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

Map Log over first and Log10 over last column:

Query[All, {1 -> Log, -1 -> Log10}] @ list;

To Query rows omit the All:

Query[{1 -> Log, -1 -> Log10}] @ list
$\endgroup$
1
$\begingroup$
list={{0.01, 0.037348}, {0.03, 0.165434}, {0.1, 0.263921}, {0.3, 
  0.560191}, {1., 0.968857}, {3., 1.50965}, {10., 2.36502}, {30., 
  3.07659}, {100., 3.73412}, {300., 4.4931}, {1000., 5.06818}, {3000.,
   5.65423}, {10000., 6.00944}}

Cases[list, {a_, b_} :> {Log[a], b}, 1]
Replace[list, {a_, b_} :> {Log[a], b}, {1}]
SequenceReplace[list, {{a_, b_}} :> {Log[a], b}]
{list[[All, 1]] // Log, list[[All, 2]]} // Transpose
MapThread[Compose, {{Log, Identity}, #}] & /@ list

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

$\endgroup$

Your Answer

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

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