0
$\begingroup$

If i have a list such as:

data = {{{274.33`, 98.90028`}, {274.33`, 98.02906`}}, {{457.645`, 
     98.02906`}, {457.645`, 85.9109`}}, {{492.329`, 
     85.9109`}, {492.329`, 81.68262`}}, {{681.938`, 
     81.68262`}, {681.938`, 80.13062`}}, {{862.401`, 
     80.13062`}, {862.401`, 78.64416`}}, {{985.038`, 
     78.64416`}, {985.038`, 77.63547`}}, {{1087.85`, 
     77.63547`}, {1087.85`, 76.49579`}}, {{1117.983`, 
     76.49579`}, {1117.983`, 76.47131`}}};

I want to change every second number in the inner-most part, that is all those that are selected by a command:

data[[All, All, 2]]

I know that I can do eg.:

data[[All, All, 2]]=data[[All, All, 2]]+100

But I would like to do with a single command operating on the whole list, so that the whole changed list is returned. I was looking at something like:

ReplacePart[#, {_, 2} :> 100] & /@ data[[All, 2]]

This works but i cannot reference the value, it just replaces that. The manual talks about using something like {i_, i_} :> f[i] but it does not work in this case as it only references diagonal elements in a matrix and whatever other form of that variant i do, i keep getting strange results such as:

{{{101, 102}, {101, 102}}, {{101, 102}, {101, 102}}, {{101, 
   102}, {101, 102}}, {{101, 102}, {101, 102}}, {{101, 102}, {101, 
   102}}, {{101, 102}, {101, 102}}, {{101, 102}, {101, 102}}, {{101, 
   102}, {101, 102}}}

How do I do this problem?

$\endgroup$
4
  • 3
    $\begingroup$ SubsetMap[# + 100 &, data, {All, All, 2}] $\endgroup$
    – Syed
    Sep 16 at 16:25
  • 3
    $\begingroup$ MapAt[# + 100 &, data, {All, All, 2}] $\endgroup$
    – Syed
    Sep 16 at 17:05
  • 1
    $\begingroup$ Try: data /. {x1_Real, x2_} -> {x1, x2 + 100} $\endgroup$ Sep 16 at 18:17
  • $\begingroup$ ConstantArray[{0, 100} , {Dimensions[data][[1]] , Dimensions[data][[2]]} ] + data $\endgroup$
    – Syed
    Sep 17 at 2:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.