6
$\begingroup$

We have the following list

data = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,-1,14}}

If I want to create a new list containing the first and the fourth elements of the first list we use

data2 = data[[All, {1, 4}]];

Now, what if I want the first and the Log10() of the fourth element? Log10(-1) does not exist so in the new list there should be only two sublists.

Any ideas?

$\endgroup$
3
  • $\begingroup$ This seems like an extremely localized problem, but would Cases[data[[All, {1, 4}]], {_?Positive, _?Positive}] help you along? $\endgroup$
    – MarcoB
    Oct 20, 2015 at 16:33
  • 2
    $\begingroup$ Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; y > 0] $\endgroup$ Oct 20, 2015 at 16:37
  • $\begingroup$ @belisariusisforth Nice work! Please write a short answer so as to approve it. $\endgroup$
    – Vaggelis_Z
    Oct 20, 2015 at 16:41

3 Answers 3

10
$\begingroup$
data = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,-1,14}}
Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; y > 0]
(* {{1, Log[4]/Log[10]}, {6, Log[9]/Log[10]}} *)

edit

Or if you want it working for expressions too:

data = {{1, 2, 3, 4, 5}, {6, 7, 8, -1, 10}, {11, 12, 13, a, 14}};
Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; Positive[y] =!= False]
(* {{1, Log[4]/Log[10]}, {11, Log[a]/Log[10]}} *)
$\endgroup$
7
$\begingroup$

Here's my take:

Cases[data, {x_, Repeated[_, {2}], y_?Positive, __} :> {x, Log10[y]}]
$\endgroup$
6
$\begingroup$

Using belisarius/J.M. but with the new operator form of Cases

Cases[{x_, _, _, y_?Positive, _} :> {x, Log10[y]} ] @ data
$\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.