MATLAB: Guassian Function, for loop only runs once??

for loopguassian elimination

a=[A,B];
for i=1:4;
a(2,i)=a(2,i)-(a(2,1)/a(1,1)).*a(1,i)
end
This is one of my first lines in my function but I do not know what this for loop only runs once A is a 3X3 matrix and B is a 3X1 which I combined for an augmented matrix

Best Answer

You're probably indexing incorrectly and instead of using the variable 'i', you're accidentally using a hard-coded value (maybe 1, by accident?)
Here's why your values aren't changing.
On the first iteration, you are replacing the value a(2,1) with a zero (see below)
i = 1
a(2,i) = a(2,i)-(a(2,1)/a(1,1)).*a(1,i)
% |______________________| this section is merely (Q/R) * R which just equals Q on the first iteration
% that reduces to
a(2,i) = a(2,i) - a(2,i) % which equals 0
On all following iterations, you're using that zero in the numerator of a fraction which will always equal 0 (see below)
i = 1 %, 3, 4
a(2,i) = a(2,i)-(a(2,1)/a(1,1)).*a(1,i)
% ( 0 /a(1,1)).*a(1,i) and that will always equal 0
% so = a(2,i) - 0 that will never change the value of a(2,i)
All of this results in only 1 change and that change happens on the first iteration in position a(2,1).
Related Question