Info
此问题已关闭。 请重新打开它进行编辑或回答。
Help required with deleting rows of a matrix with similar first 2 elements (after summing up the last elements of rows to be deleted)
1 次查看(过去 30 天)
显示 更早的评论
[1069 12059 100200 145
1069 12063 100200 471
1073 1001 100100 213
1073 1001 100101 213
1073 1007 100100 633
1073 1007 100101 633]
This is a portion of the matrix. As you can see that 1073-1001 and 1073-1007 is repeated twice (which is not true for the entire matrix; we might have 3 such pairs for eg: 1073-1008 may occur thrice below this). I want their to be only one row for 1073-1001 and 1073-1007 with the elements in column 4 being 213+213=426 and 633+633=1266, respectively.
So I want the output to be like:
[1069 12059 100200 145
1069 12063 100200 471
1073 1001 100100 426
1073 1007 100100 1266]
The actual matrix has dimensions 10730x4
Having just started using matlab I am facing severe difficulties in solving this problem. Any help will be much appreciated. Thank you.
7 个评论
Image Analyst
2013-10-1
Just attach the m-file where you create this array. Use the paper clip icon.
回答(2 个)
Jan
2013-9-30
编辑:Jan
2013-10-1
Do you mean this:
[dummy, Index] = unique(Data(:, 1:2), 'rows');
Result = Data(Index, :);
[EDITED] With adding the corresponding elements from the 4th column:
[dummy, Index] = unique(Data(:, 1:2), 'rows');
S = accumarray(Index(:), Data(:, 4));
Result = cat(2, Data(Index, 1:3), S);
3 个评论
Andrei Bobrov
2013-10-1
编辑:Andrei Bobrov
2013-10-1
a = [1069 12059 100200 145
1069 12063 100200 471
1073 1001 100100 213
1073 1001 100101 213
1073 1007 100100 633
1073 1007 100101 633];
[aa,b,c] = unique(a(:,1:2),'first','rows');
out = [aa,a(b,3),accumarray(c,a(:,4))];
another variant only for your example
[~,b2,c2] = unique(a(:,end),'first');
[~,ii] = sort(b2);
out2 = [a(b2(ii),1:end-1), accumarray(ii(c2),a(:,end))];
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!