why is my code printing my matrix?

조회 수: 5 (최근 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