Prev: initialize array of structs if don't know fieldnames
Next: Fitting the hyperbola Ax+Bxy+Cy+D=0 to experimental data
From: Chalat on 14 Jan 2010 03:28 http://courses.cit.cornell.edu/bionb441/CA/ From above link, the cellular automata. In the topic 8 Sandpile. This is the code. ............................................................................................................ %sand pile clear all clf nx=52; %must be divisible by 4 ny=100; Pbridge = .05; z=zeros(nx,ny); o=ones(nx,ny); sand = z ; sandNew = z; gnd = z ; gnd(1:nx,ny-3)=1 ; % the ground line gnd(nx/4:nx/2+4,ny-15)=1; %the hole line gnd(nx/2+6:nx,ny-15)=1; %the hole line gnd(nx/4, ny-15:ny) = 1; %side line gnd(3*nx/4, 1:ny) = 1 ; imh = image(cat(3,z',sand',gnd')); set(imh, 'erasemode', 'none') axis equal axis tight for i=1:1000 p=mod(i,2); %margolis neighborhood sand(nx/2,ny/2) = 1; %add a grain at the top %upper left cell update xind = [1+p:2:nx-2+p]; yind = [1+p:2:ny-2+p]; vary = rand(nx,ny)<.95 ; vary1 = 1-vary; sandNew(xind,yind) = ... gnd(xind,yind).*sand(xind,yind) + ... (1-gnd(xind,yind)).*sand(xind,yind).*sand(xind,yind+1) .* ... (sand(xind+1,yind+1)+(1-sand(xind+1,yind+1)).*sand(xind+1,yind)); sandNew(xind+1,yind) = ... gnd(xind+1,yind).*sand(xind+1,yind) + ... (1-gnd(xind+1,yind)).*sand(xind+1,yind).*sand(xind+1,yind+1) .* ... (sand(xind,yind+1)+(1-sand(xind,yind+1)).*sand(xind,yind)); sandNew(xind,yind+1) = ... sand(xind,yind+1) + ... (1-sand(xind,yind+1)) .* ... ( sand(xind,yind).*(1-gnd(xind,yind)) + ... (1-sand(xind,yind)).*sand(xind+1,yind).*(1-gnd(xind+1,yind)).*sand(xind+1,yind+1)); sandNew(xind+1,yind+1) = ... sand(xind+1,yind+1) + ... (1-sand(xind+1,yind+1)) .* ... ( sand(xind+1,yind).*(1-gnd(xind+1,yind)) + ... (1-sand(xind+1,yind)).*sand(xind,yind).*(1-gnd(xind,yind)).*sand(xind,yind+1)); %scramble the sites to make it look better temp1 = sandNew(xind,yind+1).*vary(xind,yind+1) + ... sandNew(xind+1,yind+1).*vary1(xind,yind+1); temp2 = sandNew(xind+1,yind+1).*vary(xind,yind+1) + ... sandNew(xind,yind+1).*vary1(xind,yind+1); sandNew(xind,yind+1) = temp1; sandNew(xind+1,yind+1) = temp2; sand = sandNew; set(imh, 'cdata', cat(3,z',sand',gnd') ) drawnow end .............................................................................................................. So, I want to add more new sand types and specify color for each types at least 5 types. Please suggest me how to do it ? Thank you very much Regard,
From: Nathan on 14 Jan 2010 13:25
On Jan 14, 12:28 am, "Chalat " <ninechala...(a)hotmail.com> wrote: > http://courses.cit.cornell.edu/bionb441/CA/ > > From above link, the cellular automata. > In the topic 8 Sandpile. > > This is the code. > ............................................................................................................ > %sand pile > clear all > clf > > nx=52; %must be divisible by 4 > ny=100; > > Pbridge = .05; > > z=zeros(nx,ny); > o=ones(nx,ny); > sand = z ; > sandNew = z; > gnd = z ; > gnd(1:nx,ny-3)=1 ; % the ground line > gnd(nx/4:nx/2+4,ny-15)=1; %the hole line > gnd(nx/2+6:nx,ny-15)=1; %the hole line > gnd(nx/4, ny-15:ny) = 1; %side line > gnd(3*nx/4, 1:ny) = 1 ; > > imh = image(cat(3,z',sand',gnd')); > set(imh, 'erasemode', 'none') > axis equal > axis tight > > for i=1:1000 > p=mod(i,2); %margolis neighborhood > sand(nx/2,ny/2) = 1; %add a grain at the top > > %upper left cell update > xind = [1+p:2:nx-2+p]; > yind = [1+p:2:ny-2+p]; > vary = rand(nx,ny)<.95 ; > vary1 = 1-vary; > > sandNew(xind,yind) = ... > gnd(xind,yind).*sand(xind,yind) + ... > (1-gnd(xind,yind)).*sand(xind,yind).*sand(xind,yind+1) .* ... > (sand(xind+1,yind+1)+(1-sand(xind+1,yind+1)).*sand(xind+1,yind)); > > sandNew(xind+1,yind) = ... > gnd(xind+1,yind).*sand(xind+1,yind) + ... > (1-gnd(xind+1,yind)).*sand(xind+1,yind).*sand(xind+1,yind+1) .* ... > (sand(xind,yind+1)+(1-sand(xind,yind+1)).*sand(xind,yind)); > > sandNew(xind,yind+1) = ... > sand(xind,yind+1) + ... > (1-sand(xind,yind+1)) .* ... > ( sand(xind,yind).*(1-gnd(xind,yind)) + ... > (1-sand(xind,yind)).*sand(xind+1,yind).*(1-gnd(xind+1,yind)).*sand(xind+1,yind+1)); > > sandNew(xind+1,yind+1) = ... > sand(xind+1,yind+1) + ... > (1-sand(xind+1,yind+1)) .* ... > ( sand(xind+1,yind).*(1-gnd(xind+1,yind)) + ... > (1-sand(xind+1,yind)).*sand(xind,yind).*(1-gnd(xind,yind)).*sand(xind,yind+1)); > > %scramble the sites to make it look better > temp1 = sandNew(xind,yind+1).*vary(xind,yind+1) + ... > sandNew(xind+1,yind+1).*vary1(xind,yind+1); > > temp2 = sandNew(xind+1,yind+1).*vary(xind,yind+1) + ... > sandNew(xind,yind+1).*vary1(xind,yind+1); > sandNew(xind,yind+1) = temp1; > sandNew(xind+1,yind+1) = temp2; > > sand = sandNew; > set(imh, 'cdata', cat(3,z',sand',gnd') ) > drawnow > end > .............................................................................................................. > > So, I want to add more new sand types and specify color for each types at least 5 types. > > Please suggest me how to do it ? > > Thank you very much > > Regard, I'm not sure about adding different types of sand and all, but the way to change the color is either to change the location of "sand'" in the line: set(imh, 'cdata', cat(3,z',sand',gnd') ) %currently, sand is in the Green position of RGB To change the sand color (in turn the density of the sand) change the "1" value in the line: sand(nx/2,ny/2) = 1; %add a grain at the top to anything between 0 and 1. The lower the number, the more sand it takes to build a pile. But still, I'm not sure how you would add more new sand types. Good luck. -Nathan |