Saying Else is invalid syntax

16 ビュー (過去 30 日間)
Gurinder Punni
Gurinder Punni 2020 年 9 月 25 日
編集済み: Sindar 2020 年 9 月 25 日
So, I'm writing a function to calculate the dot product in various ways and then find the angle between 2 vectors.
However, I keep getting a parse invalid syntax error for my first else statment.
Here is the code:
It is the most bottomest else that is getting the error.
function [d1,d2,d] = dotangle(u,v)
m = length(u);
n = length(v);
if isequal(uEntries,vEntries) == 1
fprintf('both vectors have %i entries\n',n)
d1 = transpose(u)*v;
d2 = 0;
for i= 1:m
for j = 1:n
d2 = d2 + u(i,j)*v(i,j);
end
end
d = dot(u,v);
if d1 == d2 && d2 == d
fprinitf('the code is correct\n');
uMag = norm(u);
vMag = norm(v);
Theta = acosd(d/(uMag*vMag));
if 90 == closetozeroroundoff(Theta)
disp('The angle between the vectors is 90 degrees.');
elseif 0 == closetozeroroundoff(Theta)
disp('The angle between the vectors is zero');
elseif 180 == closetozeroroundoff(Theta)
disp('The angle between the vectors is 180 degrees');
else
if Theta < 90
fprintf('Angle between vectors is acute and its value theta = %d\n',Theta);
elseif Theta > 90
fprintf('Angle between vectors is obtuse and its value theta = %d\n',Theta);
end
end
end
else
disp('check the code!');
return
else
disp('the dot product is not defined')
d1 = [];
d2 = [];
d = [];
return
end
end

回答 (1 件)

Sindar
Sindar 2020 年 9 月 25 日
編集済み: Sindar 2020 年 9 月 25 日
you have two else's for the same if. Select it all and hit CTRL-i to smart indent to see:
function [d1,d2,d] = dotangle(u,v)
m = length(u);
n = length(v);
if isequal(uEntries,vEntries) == 1
fprintf('both vectors have %i entries\n',n)
d1 = transpose(u)*v;
d2 = 0;
for i= 1:m
for j = 1:n
d2 = d2 + u(i,j)*v(i,j);
end
end
d = dot(u,v);
if d1 == d2 && d2 == d
fprinitf('the code is correct\n');
uMag = norm(u);
vMag = norm(v);
Theta = acosd(d/(uMag*vMag));
if 90 == closetozeroroundoff(Theta)
disp('The angle between the vectors is 90 degrees.');
elseif 0 == closetozeroroundoff(Theta)
disp('The angle between the vectors is zero');
elseif 180 == closetozeroroundoff(Theta)
disp('The angle between the vectors is 180 degrees');
else
if Theta < 90
fprintf('Angle between vectors is acute and its value theta = %d\n',Theta);
elseif Theta > 90
fprintf('Angle between vectors is obtuse and its value theta = %d\n',Theta);
end
end
end
else
disp('check the code!');
return
else
disp('the dot product is not defined')
d1 = [];
d2 = [];
d = [];
return
end
end
  1 件のコメント
Sindar
Sindar 2020 年 9 月 25 日
also, "fprinitf('the code is correct\n');" typo

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by