problem with cellular automata code?

i found this code here, and i try to initialize it with [1 0 1 1 0 1 1] but it didn't work.
the code is long to paste it here. and how to locate the image?

 Accepted Answer

Sara - how did you try to execute the code? Based on the given link, the function requires a rule (so this is an integer from 1 to 255), an optional initial state, and an optional number of rows (iterations for the cellular automata). What is your [1 0 1 1 0 1 1] - a rule or an initial state?
For example, the command
wolfram(45)
creates an interesting image, as does some of the other examples included in the comments (from the rich vector).
If your vector is an initial state, then you would specify the rule (1-255) and supply the state vector
wolfram(45, [1 0 1 1 0 1 1], 50);
Note that since your initial state is narrow (only 7 columns) then the number of rows should be kept small too else the image will be difficult to view.

15 Comments

i try this code and didn't run,
if nargin < 3, nrows=700; end
if nargin < 2 %Use default initial state
ncols=700; A=zeros(nrows,ncols); A(1,ncols-1)=1;
else
[unused, ncols]=size(initialstate)
A=zeros(nrows, ncols); A(1,:)=initialstate;
end
rule=dec2bin(wolfrule,8);
wolfram(45, [1 0 1 1 0 1 1], 50)
for i=1:8
ru(i)=str2num(rule(i))
my origion intial value is:
[ 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0]
how and where to insert this vector? and i want to use rule 150 with this code, i try
rule=dec2bin(150,8);
and it didn't accept.
Sara - the function signature is
function wolfram(wolfrule, initialstate, brows)
The first input parameter, wolfrule, is the rule and is a number from 1-255. The second input parameter, initialstate, is the initial state of your cellular automata and is a vector (optional). The third input parameter, brows, is the number of rows/iterations for the automata (optional).
If you want to use rule 150, then you pass this value as the first input parameter. If you want to use your original initial state from above, then pass that as the second input parameter. The code will then do the necessary conversion of the rule from an integer to a binary string.
So try
initialState = [ 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0];
rule = 150;
wolfram(rule,initialState,150);
I added the 150 for the number of rows/iterations because that seemed reasonable given the state vector size. You can change this to whatever you wish.
hi Geoff i try the code , it didn't run it gives this error:
??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
Error in ==> wolfram
Sara - are you running the three lines of code from the Command Window, or have you pasted it into the wolfram.m file? If the latter, then that is incorrect and would cause the recursive error from above. Just copy
initialState = [ 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0];
rule = 150;
wolfram(rule,initialState,150);
and paste it into the Command Window (you may need to press enter/return to execute the code).
dear Geoff - i paste it in the command window and i run it, but it gave the same message. try it please and check it. but when i change the line rule=dec2bin(wolfrule,8); to rule=dec2bin(150,8);and the title (last line) to 150. it gave the picture . but i still can't provide my own intialstate.
Sara - you must have made some changes to the wolfram.m file which is causing it to fail. Delete your version of wolfram.m, download the original from the link that you provided, don't modify the code, and then run the above three lines in the Command Window.
I'm not sure what you mean by but when i change the line ... the title (last line) to 150 because it seems to imply that you have modified the function signature and so have removed the input parameter named wolfrule and either replaced it with something else or it has disappeared entirely.
The first line in the file wolfram.m is
function wolfram(wolfrule, initialstate, brows)
dear Geoff sorry for being late..i was not around for a while. i downloaded again and i paste the code in command window and it runs. thanks alot take care
can anyone give the matlab code for cellular automata (rule 30) with image as input? Thanks in advance.
Catherine - please post this as a separate question and be clear about what you want. Clarify why the wolfram code from the File Exchange (discussed above) does not meet your need for rule 30, and clarify what you mean by with image as input.
Thank you for your reply. my task is to apply rule 30 to any of the image. how can i proceed with this problem?
To any what of the image? A row or column? Is your image grayscale or RGB?
the image i have taken is an DICOM grayscale image. rule can be applied to either row or column.
Cathrin - I suspect that you would call the wolfram function as
wolfram(30, myRowOrCol, nRowsOrCols)
where myRowOrCol is the row or column from your image that you want to apply the rule to, and nRowsOrCols is the number of rows or columns that you want to "create". You may need to update the wolfram function so that it returns the output which you can then copy into the image (if that is what you are trying to do?).
thank you for your response.
if nargin < 300, nrows=221; end
if nargin < 300 %Use default initial state
ncols=228; A=zeros(nrows,ncols); A(1,ncols-1)=1;
else
[unused, ncols]=size(initialstate)
A=zeros(nrows, ncols); A(1,:)=initialstate;
end
rule=dec2bin(wolfrule,8);
where i have changed row=221;col=228. i don't know whether the output is for my input image or not? how can i procced with?
cathrin - you will need to update the wolfram function to return the output A as
function [A] = wolfram(wolfrule, initialstate, grows)
As for changing the nrows and ncols, why have you chosen 221 and 228? Are these the dimensions of your original image? Because I would think that you wouldn't have to modify these numbers at all. For example, suppose you wish to apply to the 40th row of your image which is a 256x512 image. Then you would call wolfram as
A = wolfram(150,myImg(40,:),256-40+1);

Sign in to comment.

More Answers (0)

Asked:

on 21 Oct 2014

Commented:

on 23 Feb 2016

Community Treasure Hunt

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

Start Hunting!