Summing up subsequent rows if equal
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2023년 7월 19일
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
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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!