Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Help required with deleting rows of a matrix with similar first 2 elements (after summing up the last elements of rows to be deleted)

2 vues (au cours des 30 derniers jours)
faraz
faraz le 30 Sep 2013
Clôturé : MATLAB Answer Bot le 20 Août 2021
[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 commentaires
Image Analyst
Image Analyst le 1 Oct 2013
Just attach the m-file where you create this array. Use the paper clip icon.
faraz
faraz le 1 Oct 2013
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.

Réponses (2)

Jan
Jan le 30 Sep 2013
Modifié(e) : Jan le 1 Oct 2013
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 commentaires
Jan
Jan le 1 Oct 2013
Modifié(e) : Jan le 1 Oct 2013
@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 le 1 Oct 2013
Modifié(e) : faraz le 1 Oct 2013
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 le 1 Oct 2013
Modifié(e) : Andrei Bobrov le 1 Oct 2013
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))];

Cette question est clôturée.

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by