How can I merge similar rows in a matrix based on the first three columns' value.
10 次查看(过去 30 天)
显示 更早的评论
I have a very big matrix with 4 columns. The first three columns are coordinates of a point in a discrete 3D space, and the last column is the weight of that point. For example:
A = [1,1,1,0.2; 1,1,2,0.9; 1,2,1,1.2; ...]
Some of the coordinates, however, are duplicates with different weights. For example I might have:
A = [1,1,1,0.2; 1,1,1,2.3; 1,1,2,-0.3; ...]
What I want to achieve is to remove the duplicate coordinates, and use the mean of their weights as the weight for that coordinate. For example, after this operation, the last example will become:
A_new = [1,1,1,1.25; 1,1,2,-0.3; ...]
I have already written a code and it works is:
A_new = unique(A(:,1:3),"rows");
A_new = [A_new zeros(length(A_new),1)];
for i = 1:length(A_new)
coord = A_new(i,1:3);
dups = A(all(A(:,1:3)==coord,2), 4);
A_new(i,4) = mean(dups);
end
But it is very slow for large matrix (e.g., 1000000 rows). Can I optimize this code in anyway?
Thank you in advance.
Shayan
0 个评论
采纳的回答
Cris LaPierre
2022-1-2
Use groupsummary. Group by the first 3 columns, and use 'mean' to determine the value of the fourth. I find it easier to use on tables, so I convert A to a table first.
A = [1,1,1,0.2; 1,1,1,2.3; 1,1,2,-0.3];
A = array2table(A);
B = groupsummary(A,1:3,'mean',4)
0 个评论
更多回答(1 个)
Voss
2022-1-2
Generate some random data mimicking your situation:
[X,Y,Z] = ndgrid(1:2,1:3,1:2);
A = [X(:) Y(:) Z(:) rand(numel(X),1)];
A(:,3) = 1;
disp(A);
Use a loop like yours but comparing indices:
[A_new,~,ii] = unique(A(:,1:3),'rows');
A_new = [A_new zeros(size(A_new,1),1)];
for i = 1:size(A_new,1)
A_new(i,4) = mean(A(ii == i,4));
end
disp(A_new);
Or do the same thing with arrayfun():
[A_new,~,ii] = unique(A(:,1:3),'rows');
A_new(:,end+1) = arrayfun(@(i)mean(A(ii == i,4)),1:size(A_new,1));
disp(A_new);
2 个评论
Voss
2022-1-3
Good to know. I wasn't sure either of these ways would be much different than what you had in terms of speed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!