MATLAB: If else if statement problem

elseelseififif statement

function I = compvec(x,y)
%Calculates the angle
c = ((dot(x,y))/((sqrt(x(1)^2 + x(2)^2)) * (sqrt(y(1)^2 + y(2)^2))))
if x==0 | y==0
disp('Linearly dependent vectors (one of them is zero).r')
disp('{x, y} is a linearly dependent set.')
%Problem occurs here, if c = 1, else statement is displayed instead
elseif c == 1 || c == -1
disp('Linearly dependent vectors (both non-zero).')
disp('{x, y} is a linearly dependent set.')
elseif c == 0
disp('Linearly independent vectors (orthogonal).')
disp('{x, y} is a linearly independent set.')
else
disp('{x, y} is a linearly independent set.')
end

Best Answer

You can solve the issue rounding the value of c:
%Problem occurs here, if c = 1, else statement is displayed instead
elseif round(c) == 1 || round(c) == -1
disp('Linearly dependent vectors (both non-zero).')
disp('{x, y} is a linearly dependent set.')
elseif round(c) == 0
disp('Linearly independent vectors (orthogonal).')
disp('{x, y} is a linearly independent set.')
else
disp('{x, y} is a linearly independent set.')
end