Reputation: 13614
Suppose I have a vector B=[1 1 2 2]
and A=[5 6 7 4]
in the form of B says the numbers in the A that are need to be summed up. That is we need to sum 5 and 6 as the first entry of the result array and sum 7 and 4 as the second entry. If B is [1 2 1 2]
then first element of the result is 5+7 and second element is 6+4.
How could I do it in Matlab in generic sense?
Upvotes: 0
Views: 226
Reputation: 10676
A fexible and general approach would be to use accumarray()
.
accumarray(B',A')
The function accumulates the values in A into the positions specified by B.
Since the documentation is not simple to understand I will summarize why it is flexible. You can:
sum
by default)0
by default)sparse
, thus potential avoiding out of memoryUpvotes: 4