Effacer les filtres
Effacer les filtres

Create a vector summing different elements from another vector

1 vue (au cours des 30 derniers jours)
Fernando
Fernando le 8 Avr 2012
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

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by