Problem with multiple accesses in array indexing

1 回表示 (過去 30 日間)
BIASI Valentin
BIASI Valentin 2012 年 7 月 6 日
Hi everybody,
I have problems to make calculations on arrays with multiple accesses to the same indexes. For instance, if I have a simple vector
A = [7,8,9]
and I would like to make calculations several times on the same index
A([1,1]) = A([1,1]) + 1
the results shoud be equal to 9 and not to 8 (in tis case, I would like to make the computation twice). The only way I found to do this is with a simple statement :
b = [1,1];
for k = 1:length(b)
A(b(k)) = A(b(k)) +1;
end
But that is not cheap with big arrays. So, I would be pleased to know what is the solution of this.
Thank you guys.
  1 件のコメント
AC
AC 2012 年 7 月 6 日
Hi,
I think you need to give a more complicated example, because in this case, you could just do:
A(b)=A(b)+length(b);
But I'm guessing that may not generalize to your real problem...

サインインしてコメントする。

回答 (2 件)

F.
F. 2012 年 7 月 6 日
Like AC, I think that your example is too easy to give you a solution.
But I'm going to try with some supoositions.
For me
b = [ 1 1 2 2 3 1 2 3 ];
A = [ 7 8 9 ];
and you add "a" and not 1.
So (I can't make the test but ...)
[ c, ib,ic ] = unique( b )
V = arrayfun( @(D) sum( ic == D ), [ 1 : max(ic) ] );
A = A + V * a ;
but I'm not sure for the creation of V.

BIASI Valentin
BIASI Valentin 2012 年 7 月 6 日
I found a solution here
It uses the accumarray function.
Thank you for your contributions

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by