MATLAB: How to sum every elements that have the same frequency

MATLAB: How to sum every elements that have the same frequency

sum

Suppose I have 2 vectors of the same length.
A = [0.2 0.4 0.8 0.6 1.2 0.3];
B = [ 2 2 1 2 1 1];
The result that I want —> A+B = [(0.2+0.4+0.6),(0.8+1.2+0.3)].
Please help. Thanks in advance.

Best Answer

  • A = [0.2 0.4 0.8 0.6 1.2 0.3];
    B = [ 2 2 1 2 1 1];
    iwant = [sum(A(B==2)), sum(A(B==1))]
  • Add a Comment

    Your email address will not be published. Required fields are marked *