Info

This question is closed. Reopen it to edit or answer.

My code has returning issues

5 views (last 30 days)
Erik Börgesson
Erik Börgesson on 1 Sep 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
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!

Answers (1)

Adam Danz
Adam Danz on 1 Sep 2019
Edited: Adam Danz on 1 Sep 2019
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 Comments
Adam Danz
Adam Danz on 1 Sep 2019
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 on 1 Sep 2019
Edited: Adam Danz on 4 Sep 2019
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

This question is closed.

Community Treasure Hunt

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

Start Hunting!