I have two 2D arrays where some of the x-values are identical,
L={{1,1},{2,2},{3,3}};
J={{0,1},{2,2},{4,4}};
I want to add these lists in the following way
{{0,1}, {1,1},{2,4},{3,3},{4,4}};
In other words, that y-values of the data points that have the same x are added. How can I achieve this?
Please note that the arrays are not necessarily of the same length
Two arrays for test:
first = {{0.15, 0.000470185}, {0.16, 0.000521784}, {0.17, 0.000580663}, {0.18,
0.000648083}, {0.19, 0.000725569}, {0.2, 0.000814968}, {0.21,
0.000918532}, {0.22, 0.00103901}, {0.23, 0.0011798}, {0.24,
0.00134508}, {0.25, 0.00154005}, {0.26, 0.00177121}, {0.27,
0.00204667}, {0.28, 0.00237667}, {0.29, 0.00277409}, {0.3,
0.00325523}, {0.31, 0.00384059}, {0.32, 0.00455591}, {0.33,
0.00543312}, {0.34, 0.00651098}, {0.35, 0.00783476}, {0.36,
0.00945361}, {0.37, 0.0114133}, {0.38, 0.0137402}, {0.39,
0.0164132}, {0.4, 0.0193217}, {0.41, 0.0222235}, {0.42,
0.0247384}, {0.43, 0.0264243}, {0.44, 0.0269371}, {0.45,
0.0261818}, {0.46, 0.024341}, {0.47, 0.0217652}, {0.48,
0.0188176}, {0.49, 0.0157753}, {0.5, 0.0128044}, {0.51,
0.00998009}, {0.52, 0.00731676}, {0.53, 0.00479353}, {0.54,
0.00237078}, {0.55, 0}}
second = {{-0.04, -0.0000547619}, {-0.03, -0.000405238}, {-0.02, -0.00207438}, \
{-0.01, -0.00878929}, {0, -0.0154401}, {0.01, -0.0178195}, {0.02, \
-0.0180879}, {0.03, -0.0173267}, {0.04, -0.0157714}, {0.05, \
-0.014605}, {0.06, -0.0131757}, {0.07, -0.0119764}, {0.08, \
-0.010695}, {0.09, -0.00946286}, {0.1, -0.00849357}, {0.11, \
-0.00708071}, {0.12, -0.00578286}, {0.13, -0.00468214}, {0.14, \
-0.00306119}, {0.15, -0.00146214}, {0.16, 0.00061881}, {0.17,
0.00194733}, {0.18, 0.00376762}, {0.19, 0.00509833}, {0.2,
0.00625381}, {0.21, 0.00762286}, {0.22, 0.00918905}, {0.23,
0.0105964}, {0.24, 0.0118067}, {0.25, 0.0134238}, {0.26,
0.0149774}, {0.27, 0.0163519}, {0.28, 0.017469}, {0.29,
0.0167517}, {0.3, 0.0102788}, {0.31, 0.00253876}, {0.32,
0.000314333}, {0.33, 0.000038881}, {0.34, 0.0000224524}}
first array is here: http://www.datafilehost.com/d/731a267e.
The second array I generate from this code:
factor = 11.0;
second = Table[{(38 - i)*0.01, 0}, {i, 0, 38}] - 0.04;
tempData = (1/10000) (factor/21) {
0.41,
0.71,
5.74,
46.36,
187.70,
305.90,
319.0,
298.60,
273.50,
245.13,
215.60,
193.50,
167.80,
139.20,
114.20,
93.10,
68.80,
35.56,
11.30,
-26.70,
-55.90,
-85.50,
-105.60,
-129.30,
-155.10,
-172.80,
-195.30,
-218.70,
-240.60,
-266.70,
-288.00,
-316.4,
-330.3,
-325.4,
-281.95,
-160.50,
-37.88,
-7.4,
-1.0
};
For[i = 1, i < Length[tempData] + 1, i++,
second[[i, 2]] = tempData[[i]]];
second = second // Reverse;
0.17 ~= .16999999999999998
. So it doesn't merge them. $\endgroup$first = N@Round[first, 10^-15];
. Same forsecond
and you are all good :) $\endgroup$