필터 지우기
필터 지우기

pick arrays from matrix with condition

조회 수: 1 (최근 30일)
Tomas
Tomas 2012년 5월 11일
Hi, I have a matrix:
2 2 2 1
2 2 1 1
1 1 9 1
2 2 0 1
I need to pick rows that have same first number and sum them, but order of picking is important. In this case i need to pick first two rows and sum, then pick third and finaly fourth row; after that all the first numbers of column of new matrix should be the same depending on matrix sumed, so in the end there should be:
2 4 3 2
1 1 9 1
2 2 0 1
Any suggestions?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 12일
I = cumsum([true;diff(a(:,1))~=0]);
ii = fullfact(size(a));
out = accumarray([I(ii(:,1)), ii(:,2)],a(:))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by