Create a vector summing different elements from another vector

3 次查看(过去 30 天)
Hi,
I'm trying to do the following. Suppose you have a vector A=ones(10,1) and another vector id=[1 1 1 2 2 3 3 3 3 3]'. I would like to create a vector C that should sum the elements of A according to the vector id. The dimmensions of C should be 10 times 1, so C should be C=[3 3 3 2 2 5 5 5 5 5]'. With C=accumarray(id,A) I get C=[3 2 5]', but I don't know how to expand it to get the write dimmensions. This is just an example. In the real case the dimmensions of A are 40000 times 1 and id takes values from 1 to 12000. I was able to do some manipulations with repmat that implied generating a matrix to use id, but in my computer this only works up to 20000 rows (at that point the matrix is of dimmensions 20000 times 12000) and I get a message saying "??? Out of memory", so I need to stay in the vector case. Thanks,
Fernando

采纳的回答

Walter Roberson
Walter Roberson 2012-4-8
Index the result of accumarray with the id in order to get the extended vector you are looking for.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by