3
$\begingroup$

If I have the following list:

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 the list (e.g. 0.01, 0.03.....10000) only while leaving the second one intact? . I would like this to apply to other lists as well of different lenghts.

$\endgroup$
5
  • 3
    $\begingroup$ {Log[First[#]],Last[#]}&/@list will make a new list with log of the first element. $\endgroup$ – flinty Jun 28 '20 at 1:26
  • $\begingroup$ This works great! @flinty. Thank you very much !!! $\endgroup$ – John Jun 28 '20 at 1:29
  • 1
    $\begingroup$ Or {Log@#1, #2} & @@@ list. There are many similar questions and this will be signed as a duplicate. $\endgroup$ – Artes Jun 28 '20 at 1:50
  • 1
    $\begingroup$ list[[All,1]]=Log@list[[All,1]] $\endgroup$ – OkkesDulgerci Jun 28 '20 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$ – Michael Seifert Jun 28 '20 at 14:04
5
$\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$
9
$\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$
5
$\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$

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.