MATLAB: How to increment matrix elements using vectors of indexes

matrix manipulation

Suppose I have matrix A and vectors M,N of indexes.
I want to increment every element in A according to the pairs of M & N.
I have tried:
A(M,N) = A(M,N) + 1;
The problem is that when a pair of indexes occurs more than once I get incrementation of only 1.
for example:
A = zeros(2,2);
M = [1 1]
N = [1 1]
A(M,N) = A(M,N) + 1;
The result is:
[1 0
0 0]
instead of:
[2 0
0 0]
Any solution??
Thanks, Eli.

Best Answer

A = A + accumarray([M(:),N(:)],1,size(A));