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 个评论
faraz
faraz 2013-10-1
Attached the code. Thanks. I'm assuming by m-file you meant the code I was using because thats the only m-file i could find.

回答(2 个)

Jan
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 个评论
Jan
Jan 2013-10-1
编辑:Jan 2013-10-1
@faraz: I cannot guess, where the error comes from. Please post a small example, which reproduces the problem.
I've omitted the line to sum the values, because I was not sure if I understand what you are searching. See [EDITED].
faraz
faraz 2013-10-1
编辑:faraz 2013-10-1
Hi Jan,
I am sorry. I copied the original (unedited) code you gave incorrectly. Now it is not giving any errors, however, it is not changing my data file at all. The number of elements and everything remains the same. I assume that the purpose of the unedited code was to remove all rows that have the same elements in the first and second column. Shouldn't that be doing something?
The edited code with accumurray gives the following error:
"Error using accumarray Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
Error in untitled2task2test (line 80) S = accumarray(Index(:), pairs3(:, 4));"
Where my matrix is called pairs3 and you called it data.
Thank you for your patience. Its just that I am new to these forums and matlab in general and am facing a lot of difficulty in accomplishing this task.
[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]

Andrei Bobrov
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))];

此问题已关闭。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by