MATLAB: Comparing two matrix element simultaneously and adding third column

comparinglogical?MATLABmatrix

whenever the elemnts in column 1 and 3 both are repeated elsewhere in matrix then only third column should get added up
e.g
A=
18 18 1
17 20 2
18 18 1
17 25 2
19 17 3
18 18 1
19 17 3
then answer should be
18 18 1
17 20 2
18 18 2
17 25 2
19 17 3
18 18 3
19 17 6

Best Answer

  • A = [ 18 18 1
    17 20 2
    18 18 1
    17 25 2
    19 17 3
    18 18 1
    19 17 3] ;
    [c,ia,ib] = unique(A(:,1:2),'rows') ;
    B = A ;
    % [c,ia,ib] = unique(A(:,3)) ;
    for i = 1:length(c)
    B(ib==i,3) = cumsum(A(ib==i,3)) ;
    end
    B