I have the following code snipped which works but is incredibly slow:
bps = [5; 10; 15; 20]
src = ['10.0.0.1'; '10.0.0.2'; '10.0.0.1'; '10.0.0.2']
uniqueSrc = unique(src);
sumBps = [];
for i=1:length(uniqueSrc)
indy=find(ismember(src,uniqueSrc(i)));
sumBps = [sumBps; sum(bps(indy))];
end
uniqueSrc = ['10.0.0.1'; '10.0.0.2']
sumBps = [20; 30]
src is a cell array containing IP adresses while one IP can occur multiple times. It is read from a file with textscan and %s. bps contains integers.
I need to sum up all integers in bps that belong to the same IP adress in src. The matching is according to the indices. So src(1) is the IP of bps(1) and so on.
The result should be a matching of the IPs to the sum of the corresponding bps values. So uniqueSrc(1) is the IP that has a sumBps(1) that is the sum of all bps values belonging to the certain IP.
While my code certainly works it is very unefficient as it seems and i wonder what would be the famous matlab one-liner to solve this problem.
Thanks in advance!
Edit: Added example input and output.