Combining duplicate data point in a data array wile keeping corresponding data

11 views (last 30 days)
Commented: Star Strider on 11 Dec 2020
Hello,
I have two vectors x and y with corresponding data points. x is time data points, but may have double entries, e.g.
In fact, at the same time I am receiving several data point in spatial space (3D) (xyz direction). I want to merge identical data points for time while keeping the coressponding data points on y. For example, I wanna merge two 18 in one cell and then correspond (11.92771 -2.64893 -39.8758) and (4.938995 -9.26646 -24.8211) to 18. Should I use the unique function and then put them in struct?
t = x = [18, 18, 122, 283, 307, 346, 419, 436, 436, 436, 515, 553, 553, 557];
y = [ 11.92771 -2.64893 -39.8758,
4.938995 -9.26646 -24.8211
6.862585 1.986973 -17.4796
76.52667 31.68333 -70.2106
141.1145 42.40272 -194.467
-0.35181 -0.37868 1.738887
-3.98857 0.316846 -0.92413
-0.28841 1.157895 -1.70336
-1.7942 -0.98816 0.185217
6.728915 2.72758 -3.08762
-1.83741 -0.87261 0.316796
4.303505 4.844078 -16.5678
75.49939 -18.8806 -80.9268
11.6088 9.274039 -19.3556 ];
I want to merge identical data points for time while keeping the coressponding data points on y. For example, I wanna merge two 18 in one cell and then correspond (11.92771 -2.64893 -39.8758) and (4.938995 -9.26646 -24.8211) to 18. Should I use the unique function and then put them in struct?

Star Strider on 10 Dec 2020
What do you want to do with your data?
Unless the function you want to use them with requires unique data for ‘x’ (I see other duplicates as well), I would just keep them as they are, and then make whatever adjustments may be necessary to meet the requirements of a particular function. That way, you still have all the data, and the individual adjustments you may make to it (such as taking the mean of multiple observations) works when required.
A straightforward way to calculate the mean column values for each entry (if necessary for a particular function) is:
x = [18, 18, 122, 283, 307, 346, 419, 436, 436, 436, 515, 553, 553, 557];
y = [11.92771 -2.64893 -39.8758,
4.938995 -9.26646 -24.8211
6.862585 1.986973 -17.4796
76.52667 31.68333 -70.2106
141.1145 42.40272 -194.467
-0.35181 -0.37868 1.738887
-3.98857 0.316846 -0.92413
-0.28841 1.157895 -1.70336
-1.7942 -0.98816 0.185217
6.728915 2.72758 -3.08762
-1.83741 -0.87261 0.316796
4.303505 4.844078 -16.5678
75.49939 -18.8806 -80.9268
11.6088 9.274039 -19.3556 ];
[Ux,~,ix] = unique(x(:));
ymeans = accumarray(ix, (1:numel(x)).', [], @(x){mean(y(x,:),1)});
RowMeans = [Ux, cell2mat(ymeans)]
producing:
RowMeans =
18.0000 8.4334 -5.9577 -32.3484
122.0000 6.8626 1.9870 -17.4796
283.0000 76.5267 31.6833 -70.2106
307.0000 141.1145 42.4027 -194.4670
346.0000 -0.3518 -0.3787 1.7389
419.0000 -3.9886 0.3168 -0.9241
436.0000 1.5488 0.9658 -1.5353
515.0000 -1.8374 -0.8726 0.3168
553.0000 39.9014 -7.0183 -48.7473
557.0000 11.6088 9.2740 -19.3556
.
Star Strider on 11 Dec 2020
As always, my pleasure!