Matrix linear indexing: how to add more than once to the same index

2 ビュー (過去 30 日間)
Pablo García Auñón
Pablo García Auñón 2016 年 8 月 25 日
Here is my question. Let's imagine we have this matrix:
A = [1 2 ; 3 4];
So if I use linear indexing to add 1 to, for instance, positions 1 and 3:
A([1 3]) = A([1 3]) + 1
A =
2 3
3 4
Now, let's imagine that, having the initial A matrix, I have an index vector in which some indices are repeated. For example, I need to add twice 1 to the first position, and once to the index 3:
A([1 3 1])=A([1 3 1])+1
A =
2 3
3 4
And surprisingly I get A(1,1)=2, instead of A(1,1)=3. How can I make it work?
The first idea was to put it inside a loop, but the matrix is normally big and this operation runs many times, and it becomes very slow. Any ideas?
Thank you very much in advance!
  2 件のコメント
per isakson
per isakson 2016 年 8 月 25 日
"And surprisingly I get A(1,1)=2, instead of A(1,1)=3." &nbsp Matlab obviously doesn't work the way you think it should. I cannot find any support for this behavior in the documentation. Neither that Matlab should work this way, nor should not. In cases like this Matlab rules ;-)
>> B = 1
B =
1
>> B([1,1,1]) = B([1,1,1])+1
B =
2
Guillaume
Guillaume 2016 年 8 月 25 日
The behaviour is not surprising at all and totally expected.
With
A = [10 20; 30 40];
A([1 3 1]) is the content of indices 1, 3 and 1, so: [10 30 10]. Therefore A([1 3 1]) + 1 is the vector [11 21 11].
You then ask matlab to assign that vector to A([1 3 1]), so A ends up being
[11 21; 30 40]

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

採用された回答

Guillaume
Guillaume 2016 年 8 月 25 日
Another option, possibly faster than a loop or unique + histc:
A = [10 20;30 40];
idx = [1 3 1];
A(:) = A(:) + accumarray(idx(:), 1, [numel(A), 1]);
  2 件のコメント
dpb
dpb 2016 年 8 月 26 日
Yeah, but I thought it "cute"... :)
I always have a heckuva' time coming up w/ "the right stuff" with accumarray...
Pablo García Auñón
Pablo García Auñón 2016 年 8 月 26 日
Thanks Guillaume! I speeded it up 40 times =D

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

その他の回答 (2 件)

dpb
dpb 2016 年 8 月 25 日
編集済み: dpb 2016 年 8 月 25 日
>> ix=[1 3 1];
>> A(unique(ix))=A(unique(ix))+histc(ix,unique(ix))
A =
3 3
3 4
>>
Keep a temp for the unique index to eliminate the duplication; not sure if the JIT optimizer will find it or not if don't...

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 25 日
A = [1 2 ; 3 4]
idx=[1 3 1]
for k=1:numel(idx)
A(idx(k))=A(idx(k))+1
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by