MATLAB Answers


pick arrays from matrix with condition

Asked by Tomas
on 11 May 2012

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?


Log in to comment.


No products are associated with this question.

1 Answer

Answer by Andrei Bobrov
on 12 May 2012
 Accepted Answer

I = cumsum([true;diff(a(:,1))~=0]);
ii = fullfact(size(a));
out = accumarray([I(ii(:,1)), ii(:,2)],a(:))


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!