I had a code with nested for-loops
for a = 1:P
for i = 1:p
for j = 1:p
A(C(a, i), C(a, j)) = A(C(a, i), C(a, j)) + M(i, j);
end
end
end
I managed to ameliorate the time complexity
i = 1:p;
j = 1:p;
for a = 1:P
A(C(a, i), C(a, j)) = A(C(a, i), C(a, j)) + M(i, j);
end
However, I cannot use the same method for the last for loop because of array dimension errors. The constant P is also significantly larger than p, which makes the first for loop the most important to optimize.
How can I vectorise the last loop?
As an example of arbitrary values:
p = 2;
P = 5;
A = sparse(P+1, P+1); % A is a 6x6 matrix here. Sparse since we do not expect many non-zeros
C = [1 3; 3 5; 5 2; 2 4; 4 6]; % Pxp matrix with largest term = P+1
M = [3 6; 8 3]; % pxp matrix
The result of the for loop should be:
A(C(1, 1:2), C(1, 1:2)) += M(1:2, 1:2);
A(C(2, 1:2), C(2, 1:2)) += M(1:2, 1:2);
.
.
.
A(C(5, 1:2), C(5, 1:2)) += M(1:2, 1:2);