function [newMatrix, atBurrow] = burrowRun2d(M, vel, burrowX, burrowY) % burrowRun2d - This function will take the data of a group in standard format % (identifying row, x-value, y-value) and a "burrow point" located at (burrowX, burrowY) % and will timestep one step towards the burrow. In other words, the group will all % move at one vel towards the burrow. The output is the new group matrix and % a list of the animals that have made the journey successfully. new = []; newM = []; home = []; temp = []; D = []; A = []; l = 0; for i = 1: size(M,1) if dist2d(M(i,2), M(i,3), burrowX, burrowY) <= vel % Check to see if the indiv has reached the burrow [home] = [home; M(i,1)]; % If so, add him to the list "home" end end for i = 1:size(M,1) incl = 1; for j = 1:size(home,1) TESTLOOPJ = j% and remove him from the Matrix if i == home(j) TEST = home(j) incl = 0; end end if incl == 1 TEST2 = home(j) temp = [temp; M(i,:)]; end end newM = temp; % At this point we should only have the individuals left in the matrix who didn't reach the burrow. % We now calculate their new position, using vector addition for i = 1:size(newM,1) P = [newM(i,2), newM(i,3)]; % P is point B = [burrowX, burrowY]; % B is burrow D = B - P; % D is the vector from the point to the burrow l = dist2d(newM(i,2), newM(i,3), burrowX, burrowY); % l is the length of the vector D new = ((vel/l)*D) + P; % calculates the new point along D newM(i,2) = new(1,1); % assigns the new X value newM(i,3) = new(1,2); % assigns the new Y value end newMatrix = newM; atBurrow = home;