Create a vector summing different elements from another vector

5 ビュー (過去 30 日間)
Fernando
Fernando 2012 年 4 月 8 日
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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by