I have an n-number of Lists of equal sizes.
I want to perform different operations on different positions (sub-elements) on these lists (but avoid using For
, Do
etc. as using Thread
, and MapThread
etc. should be faster).
- First sub-elements are the same in all n-list and in the final output it will remain the same.
- Second sub-elements are added.
- Third sub-elements are added in quadrature and finally, we take the square root of that.
In principle, it is not limited to these operations only, but this is an example to show the question.
Using inspiration from Difference of certain elements from two lists I tried the following:
list1={{A1,A11,A12},{B1,B11,B12},{C1,C11,C12}};
list2={{A1,A21,A22},{B1,B21,B22},{C1,C21,C22}};
list3={{A1,A31,A32},{B1,B31,B32},{C1,C31,C32}};
... (n-number of lists)
allList = {list1, list2, list3} (**can be extended to n-numbers**)
MapThread[Flatten@{First[#],If[SameQ[Rest[#],{}],Nothing,{Rest[+##][[1]],Rest[Sqrt[+#^2]][[2]]} ]}&,allList]
while I can achieve #1 and #2, I am struggling to get #3.
The expected output:
{{A1, A11 + A21 + A31, Sqrt[A12^2+A22^2+A32^2]},
{B1, B11 + B21 + B31, Sqrt[B12^2+B22^2+B32^2]},
{C1, C11 + C21 + C31, Sqrt[C12^2+C22^2+C32^2]}}
For a small number of lists (n=3) in this example, I can achieve this as
MapThread[Flatten@{First[#],If[SameQ[Rest[#],{}],Nothing,
{Rest[+##[[1]],Sqrt[Rest[#1][[2]]^2+Rest[#2][[2]]^2+Rest[#3][[2]]^2]} ]}&,allList]
But I want to know if there are any general approaches similar to #2 where +##
is used in order to sum up all the second elements. In this context, I was wondering how to access (if it is possible at all) Slot
#1, #2, #3, ...
etc. using a dummy variable #i
? For example I was thinking Sqrt[Sum[Rest[#i][[2]]^2],{i,1,Length[allList]}]
.