Clear Filters
Clear Filters

Summing up subsequent rows if equal

3 views (last 30 days)
neil vaz
neil vaz on 19 Jul 2023
Commented: neil vaz on 20 Jul 2023
I have tried to sum up the values of the third column if the values of the rows (considering values in the first and second column only) are equal.
The following code was used for the same.
clc
close all
data = importdata('data.xlsx');
A = data.data.liquid_sat_ampl(:,1:3);
%Get the unique combinations of rows from the first two columns
%and the rows they are in
[B,~,rowidx] = unique(A(:,1:2),'rows');
%Get the sum of values (i.e. 3rd column of A) corresponding to same indices
S = accumarray(rowidx,A(:,3));
%Final output
Out = [B S];
The result obtained is as such and is highlited in blue.
However, the actual result that I am supposed to get is 14112 that is equal. I am not sure what the error would be and why is this occuring. Help with this regard will be highly appreciated.
Thank you in advance.
  4 Comments
Dyuman Joshi
Dyuman Joshi on 19 Jul 2023
Theoretically, that makes sense.
However, the data is not according to that. I went through your data to see if there is a pattern. And I found one, the pattern being that each pair of points repeats 9 times (in a row).
But, not all data is like that. See below -
format long
A=readmatrix('data.xlsx');
for k=1:size(A,1)/9
%Data set of 9 consecutive points (1-9, 10-18, 19-27, ...)
z=size(unique(A(9*(k-1)+1:9*k,1:2),'rows'));
%Comparing the size of unique rows of each set
if ~isequal(z,[1 2])
%Index where there are 2 unique rows in a set
%evident by the values in z
k
z
A(9*(k-1)+1:9*k,1:2)
break
end
end
k =
61
z = 1×2
2 2
ans = 9×2
0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298700000000 -0.000725000000000 0.001298800000000 -0.000725000000000 0.001298800000000 -0.000725000000000
These are the points 542nd row to 550th row, 1st 2 columns. You can see that the last two data points in the first column do not match the rest of the data points. And there are many more cases of this behaviour. This is why the result you obtain is not same as what is expected.
neil vaz
neil vaz on 20 Jul 2023
I see, and this is the reason behind why I am not able to get the desired output. Thanks for your time and detailed analyis Dyuman. Have a good day!

Sign in to comment.

Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!