why is my code printing my matrix?

4 views (last 30 days)
Linnea
Linnea on 5 Oct 2023
Answered: Shreshth on 2 Jan 2024
%it keeps printing the matrix A (or B, not sure) when running, but I've added semicolons where I'd
%consider them necessary to avoid that. What have I missed?
function gameoflife(A)
[a,b]=size(A);
B=zeros(a,b);
gen=10
for generation = [1:gen]
for i=1:a
for j=1:b
n = numneighbors(A,i,j);
status = celldestiny(A,i,j,n);
B(i,j)=status;
end
end
A=B;
pause(0)
clf
plotroutine(A)
%spy(A)
end
end
  3 Comments
Fabio Freschi
Fabio Freschi on 5 Oct 2023
Difficult to say: there are many undefined functions. A self consistant minimum working example would help
Torsten
Torsten on 5 Oct 2023
According to the name of the function: most probably because you call plotroutine(A).

Sign in to comment.

Answers (1)

Shreshth
Shreshth on 2 Jan 2024
Hello Linnea,
I understand that you are experiencing an issue with the MATLAB function for Conway's Game of Life, where either matrix “A or B”is being printed during execution, despite your addition of semicolons appropriately to suppress output.
The semicolons in MATLAB are used to suppress the output of a command or statement when running scripts and functions. If you're seeing the matrix “A or B ”being printed out unexpectedly, it's possible that the output is being generated by a function or command within the loop that is missing a semicolon.
However, from the code snippet you've provided, there are no obvious missing semicolons that would cause the matrices to be printed within the main loop. The definition of “gen=10” is missing a semicolon at the end, which would print the value of “genwhen the function is called, but that would only happen once.
Here is the code snippet to add semicolon after gen declaration:
gen=10; % Added semicolon here
for generation = [1:gen]
If you're still seeing the matrices being printed, the issue might be in one of the functions called within your loop, such as ‘numneighbors’, ‘celldestiny’, or ‘plotroutine’. You should ensure that these functions also use semicolons to suppress their output where necessary.
Thank you,
Shubham Shreshth.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!