I have had customers ask me occasionally about what happens during an indexed assignment when indices are repeated. The answer is that it depends on how you do the assignment.
I initialize an output matrix to zeros and create two vectors, one with locations (indices) and one for the corresponding data. Now let's look at the results of an indexed assignment.
A = zeros(3); loc = [1 3 7 9 1]; data = 1:5; A(loc) = A(loc) + data
A = 5 0 3 0 0 0 2 0 4
You see in this case that even though index 1 was repeated, its output value is only 5, and not 6. Let me reset A so I can show you what's going on.
A = zeros(3);
Here's what "essentially" happens during an assignment in MATLAB. On the right-hand side, A(loc) is created.
ans = 0 0 0 0 0
This means that there are 2 copies of A(1) currently and an element of data will be added to each of these. The important thing to realize is that now the values for the right-hand side have been calculated and set.
A(loc) + data
ans = 1 2 3 4 5
Following this, the 5-element right-hand side is assigned into A(loc) on the left-hand side. Values are currently placed into the output vector by marching down the left-hand side locations, replacing one element at a time. So the first element gets replaced not once, but twice, with the final one being the value that you see.
A(loc) = A(loc) + data
A = 5 0 3 0 0 0 2 0 4
Here's another way to do the assignment, but in this case, the
B = zeros(3); for n = 1:length(loc) B(loc(n)) = B(loc(n)) + data(n); end B
B = 6 0 3 0 0 0 2 0 4
Notice that the output here for B does not match that from the vectorized assignment above. This is because the values for the right-hand side get updated one at a time, and can therefore accumulate as additions made to a particular index.
ans = 0
What if you want the accumulation effect, but you'd prefer to use vectorized code? That's what the function accumarray is for.
C = accumarray(loc',data,[9 1]); C = reshape(C,[3 3])
C = 6 0 3 0 0 0 2 0 4
We see the same results here as those from the for loop example.
ans = 1
Did you know this about MATLAB already? I'm curious to hear your experiences in this area. Post them here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.