# Sum up values depending on entries in another column

3 views (last 30 days)
buhmatlab on 7 Apr 2020
Edited: madhan ravi on 7 Apr 2020
Hi,
With the friendly helpf of this forum I was able to count specific values but now there is a last hurdle:
I've got a column vector 300x1 witch contains strings, say:
vector1 = [A;A;B;A;B;B;C;A;C]
and another vector with values, say:
vector2 = [5;2;0;1;4;2;1;3;1]
Now I want to sum up the values in vector 2 for each (same) string, which should look like:
vector3 = [5;7;0;8;4;6;1;11;2]
NOTE: Later on I would like to add more crterions than only the string name.
How can I achieve this?
Thank you in advance!
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

David Hill on 7 Apr 2020
vector1=['AABABBCAC']';
vector2=[5 2 0 1 4 2 1 3 1]';
vector3=zeros(size(vector2));
a=unique(vector1);
for k=1:length(a)
vector3(ismember(vector1,a(k)))=cumsum(vector2(ismember(vector1,a(k))));
end
##### 1 CommentShow -1 older commentsHide -1 older comments
buhmatlab on 7 Apr 2020
Great, this works! Thank you!!

Sign in to comment.

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!