Info

此问题已关闭。 请重新打开它进行编辑或回答。

My code has returning issues

2 次查看(过去 30 天)
Erik Börgesson
Erik Börgesson 2019-9-1
关闭: MATLAB Answer Bot 2021-8-20
function [probcornerstot,probcornersone,probcornerstwo] = xCornersPL(League,Team1,Team2,n)
[~, row1] = ismember({Team1}, League.Team);
[~, row2] = ismember({Team2}, League.Team);
Factor1 = (League.For(row1));
Factor2 = (League.Against(row2));
Team1Corners = Factor1*Factor2*5.2835
Factor3 = (League.Against(row1));
Factor4 = (League.For(row2));
Team2Corners = Factor3*Factor4*5.2825
lambda = Team1Corners + Team2Corners;
probcornerstot = zeros(1,n);
e = 2.7182818284590452;
for k = 1:n+1
probcornerstot(k) = ((lambda.^(k-1)).*(e.^(-1.*lambda)))./(factorial((k-1)))
end
probcornersone = zeros(1,n);
e = 2.7182818284590452;
for k = 1:n+1
probcornersone(k) = ((Team1Corners.^(k-1)).*(e.^(-1.*Team1Corners)))./(factorial((k-1)))
end
probcornerstwo = zeros(1,n);
e = 2.7182818284590452;
for k = 1:n+1
probcornerstwo(k) = ((Team2Corners.^(k-1)).*(e.^(-1.*Team2Corners)))./(factorial((k-1)))
end
end
Hello! What do I need to change here so that my function only will return the last iteration of ''probcornerstot'', ''probcornersone'' and ''probcornerstwo''? I am also annoyed by the code returning ''ans''. Thank you!

回答(1 个)

Adam Danz
Adam Danz 2019-9-1
编辑:Adam Danz 2019-9-1
Place semicolons (;) at the end of lines to supress the command window output. Lines that do not end with a semicolon may print output to the command window.
For example,
probcornerstot(k) = ((lambda.^(k-1)).*(e.^(-1.*lambda)))./(factorial((k-1)));
% right here ^
  2 个评论
Adam Danz
Adam Danz 2019-9-1
Erik Börgesson's answer moved here as a comment.
Hello and thank you for your answer. When I do that it only returns Team1Corners = x, Team2Corners2 = x, and a matrix of ''ans''. Do you know what more I need to change?
Thank you!
Adam Danz
Adam Danz 2019-9-1
编辑:Adam Danz 2019-9-4
That's because you still have lines that do not end in ;
For example,
Team2Corners = Factor3*Factor4*5.2825 % no ; at the end
As for the "ans" at the end of your code, when your function is done executing at least the 1st output is printed to the command window if the call to that function did not end in a semicolon.
xCornersPL(League,Team1,Team2,n) % no ; at the end
[addendum]
If all of the outputs are supressed properly, you shouldn't see anything printed to the command window. The output variables will still hold the output data, you just won't see anything printed (unless, of course, you want to see outputs, in which case you'd remove the terminal semicolons).
If you'd like to display the output after the function has completed execution, you can use
disp(VARIABLE)
% Or just
VARIABLE

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by