1
$\begingroup$

I have three pairs of list and need two divide second column of first list by second column of the second list. Furthermore, I have to do for all three pairs. My data list are tagged aa[1]...aa[3] therefore I cannot change code to change the way they are listed. Below is example,

aa[1] = {{1, 2}, {2, 1}, {3, 1}, {4, 2}, {5, 2}}; (*first pair*)
aa[2] = {{1, 1}, {2, 1}, {3, 3}, {4, 1}, {5, 0}}; 
aa[3] = {{1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2}}; 

bb[1] = {{1, 1}, {2, 4}, {3, 4}, {4, 2}, {5, 1}}; (*second pair*)
bb[2] = {{1, 1}, {2, 3}, {3, 2}, {4, 1}, {5, 1}}; 
bb[3] = {{1, 1}, {2, 1}, {3, 0}, {4, 1}, {5, 1}}; 


Do[aa[ii][[All, 2]] = aa[ii][[All, 2]]/bb[ii][[All, 2]], {ii, 3}];

But Mathematica version 9 is complaining and not doing this. What is wrong with my code? To make it work, I have to do this three times:

aa1 = aa[1];
bb1 = bb[1];
aa1[[All, 2]] = aa1[[All, 2]]/bb1[[All, 2]];

Is there a way to simplify this?

$\endgroup$
1
$\begingroup$

Using ReplacePart instead of Part assignment works:

Quiet@Do[aa[i] = ReplacePart[aa[i], {r_, 2} :> aa[i][[r, 2]]/(bb[i][[r, 2]])], {i, 3}];

{aa[1], aa[2], aa[3]} // Column

enter image description here

$\endgroup$
1
$\begingroup$

Update

Here's another way of doing things, if you want to set a[1] et. al. to the new updated lists:

{a[1], a[2], a[3]} = MapThread[#1[[All, 2]]/#2[[All, 2]] &, {Array[a, 3], Array[b, 3]}]

Original Post

How about this instead?

a = {{{1, 2}, {2, 1}, {3, 1}, {4, 2}, {5, 2}},
     {{1, 1}, {2, 1}, {3, 3}, {4, 1}, {5, 0}},
     {{1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2}}};
b = {{{1, 1}, {2, 4}, {3, 4}, {4, 2}, {5, 1}},
     {{1, 1}, {2, 3}, {3, 2}, {4, 1}, {5, 1}},
     {{1, 1}, {2, 1}, {3, 0}, {4, 1}, {5, 1}}};

Then:

MapThread[#1[[All, 2]]/#2[[All, 2]] &, {a, b}]
(* {{2, 1/4, 1/4, 1, 2}, {1, 1/3, 3/2, 1, 0}, {0, 1, ComplexInfinity, 2, 2}} *)

This will throw an error because of the dividing by zero.

$\endgroup$
  • $\begingroup$ I cannot change a[1]...to a. The reason is that already have a code to do integral and plots for a[1], a[2] and so on. Looks like I have to go for a loop. $\endgroup$ – Aschoolar Dec 19 '17 at 0:52
  • 1
    $\begingroup$ Then define aa={a[1], a[2], a[3]} and same for b. $\endgroup$ – march Dec 19 '17 at 1:05
  • $\begingroup$ Once I did the division part, how do I revert back to a[1],a[2] and a[3] from aa? a[3]=aa[[All,4]] and a[2]=aa[[All,3]] and so on. Is that right approach? $\endgroup$ – Aschoolar Dec 19 '17 at 3:15
  • $\begingroup$ @Aschoolar: I mean, sure, but what are you really trying to do? Defining three different objects a[1], a[2], and a[3] seems like overkill when you can just the list a instead. It makes things easier to automate in my opinion. In any case, I've updated my post with maybe a more straightforward way of doing things. $\endgroup$ – march Dec 19 '17 at 4:37

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.