Learning matlab !!!
Its fun unlike learning C,php,sql etc. which I am afraid of. My first experiment started with the function magic() long ago long long ago when i was i first year. It was nice to identify the pattern of numbers for odd square matrices while had to break the head for the even dimensional matrices. Then the greed made me to go through the help to understand the code that they wrote (nice algo
). It was then I found 3d plots very interesting and one day i sat to write a matrix (i forgot the dimentions of the matrix) to show A in the graph. Then I started playing with a lot of functions and started forgetting the older ones
. You keep forgetting unless you dont use them in preactice. And today probably was the first time i used the functions for a proper use. And here is my first proper matlab file
.
Here is the code (any suggestions of improvement is welcome I’ll learn frm them)
function generation=game(m)
%my first MATLAB problem statement is “GAME OF LIFE”
temp=0;
generation=0;
X=round(rand(m,m));
[i,j] = find(X);
figure(gcf);
plothandle = plot(i,j,’.', ‘Color’,'blue’);….this is nice way to write comment
axis([0 m+1 0 m+1]);
n = [m 1:m-1];
e = [2:m 1];
w = [m 1:m-1];
s = [2:m 1];
Z=0;
while 1
if((temp==0))
Y=X;
temp=1;
elseif(temp==2)
Z=X;
temp=0;
elseif(temp)
temp=temp+1;
end
if(Z==Y)
break
end
N = X(n,: ) + X(s,: ) + X(:,e) + X(:,w) + X(n,e) + X(n,w) + X(s,e) + X(s,w);
X= (X & (N == 2)) | (N == 3);
[i,j] = find(X);
set(plothandle,’xdata’,i,’ydata’,j)
drawnow
generation=generation+1;
end%the number of generation i.e the answer returned is sometimes far less
%than the actaul number of generations it takes to stabilize a population%Also here the universe is circular which could be made flat by modifying
%the values of news.
EOF
Rules of Game Of Life
In the “Game of Life” the domain is a 2-dimensional array of cells, and each cell can have one of two possible states, usually referred to as “alive” or “dead.” The array is usually intialized using random numbers and the system then evolves in time. At each time step, each cell may or may not change its state, based on the number of adjacent alive cells, including diagonals. There are three rules:
- If a cell has three neighbors that are alive, the cell will be alive. If it was already alive, it will remain so, and if it was dead, it will become alive.
- If a cell has two neighbors that are alive, there is no change to the cell. If it was dead, it will remain dead, and if it was alive, it will remain alive.
- In all other cases — the cell will be dead. If it was alive it becomes dead and if it was dead it remains dead.
June 26, 2008 at 2:25 pm
You should have disabled the smiles in the code.
June 26, 2008 at 7:14 pm
@sp2hari
I tried a lot but there was no option “disable smileys in this post”
so added a extra space it worked