Create a vector summing different elements from another vector

5 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by