I am quite new to MATLAB matrix calculation and don't know how to improve the performance when I have many for-loops. I have tried to figure it out by looking through similar questions, but I still feel puzzled.
Here is my code:
pred = zeros(n_test,1) % vector saving all prediction results
for d = 1:n_test % n_test test data
u = user(d);
p_locations = zeros(n_location,1);
for i = 1:n_location
for z = 1:n_topic
for r = 1:n_region
p_locations(i) = p_locations(i)+para.Pzu(z,u)*para.Pru(r,u)*para.Piz(i,z)*para.Pir(i,r); % calculate the probability of the location i
end
end
end
[val, pos] = max(p_locations); % find the location of the largest probability
pred(d) = pos;
end
As commented, basically, I want to predict location for each test data. Then I will compare with ground truth.
I have almost 80000 test data and the calculation is really slow. It's already 13 hours and it's still running. So could you teach me how to re-write the code? Thanks a lot!