why is my code printing my matrix?

4 次查看(过去 30 天)
Linnea
Linnea 2023-10-5
回答: Shreshth 2024-1-2
%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 个评论
Fabio Freschi
Fabio Freschi 2023-10-5
Difficult to say: there are many undefined functions. A self consistant minimum working example would help
Torsten
Torsten 2023-10-5
According to the name of the function: most probably because you call plotroutine(A).

请先登录,再进行评论。

回答(1 个)

Shreshth
Shreshth 2024-1-2
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by