필터 지우기
필터 지우기

Create a vector summing different elements from another vector

조회 수: 1 (최근 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에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by