MATLAB: Nested for loop

nested for loop

I can't figure out why the S matrix is computed to have the same value for every element – 0.7654. All other matrices (A through E) are computed correctly.
format short
x=[-0.9239, -0.6533, 0, 0.6533, 0.9239, 0.6533, 0, -0.6533]; %yj
y=[0, 0.6533, 0.9239, 0.6533, 0, -0.6533, -0.9239, -0.6533]; %yi
X=[-0.9239, -0.9239, -0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827]; %Xj
Xone=[-0.9239, -0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239];%Xj+1
Y=[-0.3827, 0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239, -0.9239]; %Yj
Yone=[0.3827, 0.9239, 0.9239, 0.3827, -0.3827, -0.9239, -0.9239, -0.3827]; %Yj+1
phi=pi/180*[90, 45, 0, 315, 270, 225, 180, 135]; %phi
for i=1:8
for j=1:8
A(i,j)=-(x(i)-X(j)).*cos(phi(j)) - (y(i)-Y(j)).*sin(phi(j));
B(i,j)=(x(i)-X(j)).^2 + (y(i)-Y(j)).^2;
C(i,j)=sin(phi(i) - phi(j));
D(i,j)=(y(i)-Y(j)).*cos(phi(i)) - (x(i)-X(j)).*sin(phi(i));
E(i,j)=(x(i)-X(j)).*sin(phi(j)) - (y(i)-Y(j)).*cos(phi(j));
S(i,j)=((Xone(j)-X(j))^2 + (Yone(j)-Y(j))^2)^0.5;
end
end

Best Answer

The way you have defined S, it is independent of index i.
S(i,j) = ((Xone(j)-X(j))^2 + (Yone(j)-Y(j))^2)^0.5; % No i appears.
Therefore it will have the same value for every element of each column. Did you mean to have no dependence on index i?
Related Question