Finding and counting of identical rows in a matrix

조회 수: 2 (최근 30일)
Aytug Bas
Aytug Bas 2019년 10월 11일
댓글: Aytug Bas 2019년 10월 11일
hello together,
I have to find the identical rows or identical lines in a matrix and sum the time values of these rows in the first column. At the end I have to create a new matrix and write only one of the identical rows and delete the rest identical rows. And I have to keep not identical rows for example
Matrix:
1 300 3500 500 6000
3 200 3000 500 6500
5 150 2500 450 6000
8 400 2000 550 5500
5 200 3000 500 6500
9 150 2500 450 6000
2 200 3000 500 6500
...
2nd, 5th and 7th rows are identical except 1st column . Then I have to add the corresponding values in the first column or 3 + 5 + 2 = 10. as well as 3rd and 6th, so 5 + 9 = 14
I have to write one of the identical rows and not identical rows. The identical lines occur more than 2 times. Matrix consists of many rows eg. 1000. The new matrix looks like this:
1 300 3500 500 6000
10 200 3000 500 6500
14 150 2500 450 6000
8 400 2000 550 5500
how can I realize this in Octave? Would anyone have an idea?
Thank you very much

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 10월 11일
편집: Andrei Bobrov 2019년 10월 11일
For Octave?
M = [ 1 300 3500 500 6000
3 200 3000 500 6500
5 150 2500 450 6000
8 400 2000 550 5500
5 200 3000 500 6500
9 150 2500 450 6000
2 200 3000 500 6500];
[a,b,c] = unique(M(:,2:end),'rows','first');
[~,i] = sort(b);
[~,ii] = sort(i);
out = [accumarray(ii(c),M(:,1)), a(i,:)];
Here use Octave 5.1.0
  댓글 수: 7
Andrei Bobrov
Andrei Bobrov 2019년 10월 11일
Mode = [...
1 100 1500 0 1750
1 100 1500 0 1750
1 100 1500 0 1750
1 100 1500 0 1750
1 100 1500 0 1750
1 100 1500 0 1750
1 100 1750 -50 2250
1 100 1750 -50 2250
1 100 1750 -50 2250
1 100 1750 -50 2250
1 100 1750 -50 2250
1 100 1500 0 2000
1 100 1500 0 2000
1 100 1500 0 2000
1 100 1500 0 2000
1 100 1500 -1 500
1 100 1500 -1 500
1 100 1500 -1 500 ];
[a,b,c] = unique(Mode(:,2:end),'rows','first');
[~,i] = sort(b);
out = [accumarray(i(c),Mode(:,1)), a(i,:)]
out =
5 100 1500 0 1750
3 100 1750 -50 2250
4 100 1500 0 2000
6 100 1500 -1 500
[a,b,c] = unique(Mode(:,2:end),'rows','first');
[~,i] = sort(b);
[~,ii] = sort(i);
out = [accumarray(ii(c),Mode(:,1)), a(i,:)]
out =
6 100 1500 0 1750
5 100 1750 -50 2250
4 100 1500 0 2000
3 100 1500 -1 500
Aytug Bas
Aytug Bas 2019년 10월 11일
Thank you very much Bobrov.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 11일
M = [ 1 300 3500 500 6000
3 200 3000 500 6500
5 150 2500 450 6000
8 400 2000 550 5500
5 200 3000 500 6500
9 150 2500 450 6000
2 200 3000 500 6500];
[G,U1,U2,U3,U4] = findgroups(M(:,2),M(:,3),M(:,4),M(:,5));
newM = [splitapply(@sum, M(:,1), G), U1, U2, U3, U4];

카테고리

Help CenterFile Exchange에서 Octave에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by