why is my code printing my matrix?

%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 件のコメント

Walter Roberson
Walter Roberson 2023 年 10 月 5 日
You have not shown us the code for numneighbors or celldestiny or plotroutine, so we might guess that those are displaying a matrix.
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 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2023 年 10 月 5 日

回答済み:

2024 年 1 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by