- #1
gryphon1221
- 9
- 0
Hi, I am trying to model an intersection using different rules but I am having a hard time getting the thing to work in the simplest stage. I know there is probably an easier way to do this but I am trying to do it this way:
clear
grid=zeros(10,10); %grid creation
grid2=zeros(10,10);
grid3=zeros(10,10);
grid(1:4,1:4)=2; %empte spaces representing city blocks
grid(7:10,1:4)=2;
grid(1:4,7:10)=2;
grid(7:10,7:10)=2;
grid(6,2)=1; %initial grid coords for cars
grid(6,4)=1;
grid(7,6)=1;
grid(5,7)=1;
grid(5,9)=1;
grid(4,5)=1;
grid(2,5)=1;
grid(9,6)=1;
tfinal=100;
for t=1:10
random=randi(10,1);
if grid(6,10)==1;
grid2(6,10)=0;
end
if grid(6,9)==1;
grid2(6,10)=1 & grid3(6,9)==-1;
end
if grid(6,8)==1;
grid2(6,9)=1 & grid3(6,8)==-1;
end
if grid(6,7)==1;
grid2(6,8)=1 & grid3(6,7)==-1;
end
if grid(6,6)==1;
grid2(6,7)=1 & grid3(6,6)==-1;
end
if grid(6,5)==1;
grid2(6,6)=1 & grid3(6,5)==-1;
end
if (grid(6,4)==1) && 0<t<=(tfinal/10)
grid2(6,5)=1 & grid3(6,4)==-1;
end
if grid(6,3)==1;
grid2(6,4)=1 & grid3(6,3)==-1;
end
if grid(6,2)==1;
grid2(6,3)=1 & grid3(6,2)==-1;
end
if grid(6,1)==1;
grid2(6,2)=1 & grid3(6,1)==-1;
elseif grid(6,1)==0 & random>=5;
grid2(6,1)=1;
end
grid2=grid3+grid2;
grid=grid2;
end
This is just the first of 4 aspects of the intersection node. I want the cars to move ahead in each column, except a time limitation on column 5 representing the signal. This is my first time using & for anything in matlab, and I have a feeling not understanding its use is causing me problems. I have been trying for a while and I am just plain stuck now. Anyone have any suggestions?
clear
grid=zeros(10,10); %grid creation
grid2=zeros(10,10);
grid3=zeros(10,10);
grid(1:4,1:4)=2; %empte spaces representing city blocks
grid(7:10,1:4)=2;
grid(1:4,7:10)=2;
grid(7:10,7:10)=2;
grid(6,2)=1; %initial grid coords for cars
grid(6,4)=1;
grid(7,6)=1;
grid(5,7)=1;
grid(5,9)=1;
grid(4,5)=1;
grid(2,5)=1;
grid(9,6)=1;
tfinal=100;
for t=1:10
random=randi(10,1);
if grid(6,10)==1;
grid2(6,10)=0;
end
if grid(6,9)==1;
grid2(6,10)=1 & grid3(6,9)==-1;
end
if grid(6,8)==1;
grid2(6,9)=1 & grid3(6,8)==-1;
end
if grid(6,7)==1;
grid2(6,8)=1 & grid3(6,7)==-1;
end
if grid(6,6)==1;
grid2(6,7)=1 & grid3(6,6)==-1;
end
if grid(6,5)==1;
grid2(6,6)=1 & grid3(6,5)==-1;
end
if (grid(6,4)==1) && 0<t<=(tfinal/10)
grid2(6,5)=1 & grid3(6,4)==-1;
end
if grid(6,3)==1;
grid2(6,4)=1 & grid3(6,3)==-1;
end
if grid(6,2)==1;
grid2(6,3)=1 & grid3(6,2)==-1;
end
if grid(6,1)==1;
grid2(6,2)=1 & grid3(6,1)==-1;
elseif grid(6,1)==0 & random>=5;
grid2(6,1)=1;
end
grid2=grid3+grid2;
grid=grid2;
end
This is just the first of 4 aspects of the intersection node. I want the cars to move ahead in each column, except a time limitation on column 5 representing the signal. This is my first time using & for anything in matlab, and I have a feeling not understanding its use is causing me problems. I have been trying for a while and I am just plain stuck now. Anyone have any suggestions?