1
$\begingroup$

I have a list with elements

{a -> -1, b -> -2, c -> -3}

If I now wanted to apply a tranformation to b and c so that they would give the tranformation b -> 1-10^val and c -> 1-10^val, yielding

{a -> -1, b -> 0.99, c -> 0.999}

How would I do this in Mathematica?

$\endgroup$
  • $\begingroup$ Ha yes apologies that's what I meant I changed my question accordingly - thanks for both of your answers they work perfectly! $\endgroup$ – Tom Wenseleers Nov 20 '15 at 16:02
4
$\begingroup$

I'm not sure whether you mean something different by 1-b^-2 or you just miscalculated, because your result is not the correct result. In general, you can transform transformation-rules like this:

{a -> -1, b -> -2, c -> -3} /. 
  {
    (b -> val_) :> (b -> 1 - val^-2),
    (c -> val_) :> (c -> 1 - val^-3)
  }

And in case Kuba is right about what you really want, then you can use

{a -> -1, b -> -2, c -> -3} /. 
  {
    (b -> val_) :> (b -> 1 - 10.0^val), 
    (c -> val_) :> (c -> 1 - 10.0^val)
  }
(* {a -> -1, b -> 0.99, c -> 0.999} *)

Or

{a -> -1, b -> -2, c -> -3} /. 
  {(key : b | c -> val_) :> (key -> 1 - 10.0^val)}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Also, val are equal to powers, which is suspicious. I suppose to formula is 1-10^val. $\endgroup$ – Kuba Nov 20 '15 at 11:59
  • $\begingroup$ Was already adding it :-) $\endgroup$ – halirutan Nov 20 '15 at 12:11
  • $\begingroup$ Many thanks for this - works perfectly! 2nd solution is what I was looking for - sorry for the typo in my question! $\endgroup$ – Tom Wenseleers Nov 20 '15 at 16:04
5
$\begingroup$
list = {a -> -1, b -> -2, c -> -3}

The quesiton is unclear but let's say I know what you want :)

MapAt[
  1. - 10^# &,
  Association[list],
  List@*Key /@ {b, c}] // Normal

{a -> -1, b -> 0.99, c -> 0.999}

You could work on Association from the begining, then you can skip Association[] and Normal which makes it even more compact.


Other way, if you know positions and don't want to use Associations:

MapAt[
  1. - 10^# &,
  list,
  {2 ;; 3, 2}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Many thanks for this - works perfectly! $\endgroup$ – Tom Wenseleers Nov 20 '15 at 16:04

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.