MATLAB: Calculation of Phi value in matlab

calculation of an angle

width=20;
length=40;
height=10;
x=1:2:20;
y=1:4:40;
z=1:1:10;
E =length/width;
alpha=atan(E);
x1= (x.*sin(alpha)) - (y .* cos (alpha));
y1= (height./2)-z;
k= y1 ./ x1;
phi1=zeros(size(k));
for ii= 1:length(k)
if k(ii)>0
phi1(ii)= atan(k(ii)); % equation 1
else
phi1(ii)= pi-atan(k(ii)); % equation 2
end
end
Error msg : ??? Subscript indices must either be real positive integers or logicals.
Sorry. I am very noob at matlab. Please help me correct with the error. I am trying to calculate the phi1 value. When k is positive it have to use the quation 1 and when k is negative it have to use the equation 2.

Best Answer

Try:
width=20;length=40;height=10;
x=1:2:20;
y=1:4:40;
z=1:1:10;
E =length/width;
alpha=atan(E);
x1= (x.*sin(alpha)) - (y .* cos (alpha));
y1= (height./2)-z;
k= y1 ./ x1;
[M,N]=size(k)
phi1=zeros(N);
for ii= 1:N
if k(ii)>0 phi1(ii)= atan(k(ii)); % equation 1 else phi1(ii)= pi-atan(k(ii)); % equation 2
end
end