I have some difficulties in converting a pseudocode into a MatLab algorithm. Specifically, there is one part I don’t quite know how to do. I will write the pseudocode up to the point where I am uncertain:
input n, (a_{ij}), (b_i), (x_i), M
for k = 1 to M do
for i = 1 to n do
u_i = (b_i - sum[(from j = 1, (j ≠ i), to n) a_{ij} * x_j]) / a_{ii}
end do
The difficulty I have is when I have to write the sum-part. What I can’t figure out is how to write the algorithm so that terms where j ≠ i are not included. So far I have written:
function [k,x] = jacobimethod(A,b,M)
n = length(A);
u = zeros(1,n);
x = zeros(1,n);
for k = 1:M
for i = 1:n
u(i) = (b(i) - (A(i
And this is where I get stuck. So far, all my algorithms have involved sums where all terms should be included, even those where j = i. In this case, the sum term will simply be:
A(i,1:n)*x(1:n)'
But how can I modify this so that A(i,i) is not included?
Any help will be greatly appreciated!
You could make an explicit index array and remove the index you don’t need