Info

This question is closed. Reopen it to edit or answer.

Trouble Isolating Rows That Meet Certain Criteria

1 view (last 30 days)
Grant Cates
Grant Cates on 18 Jun 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello all,
I'm working on a computational physics undergrad research project analyzing fly swarms. I am attempting to model said swarms using MATLAB.
I've been searching for a solution to this problem for about 2 weeks, but none of the resources online have been fruitful. Here's the structure of my program in a nutshell:
  • Generate N random, unique points on an MxM lattice
  • For each point, at each time stamp, determine the four possible moves (up, down, left, right)
  • At each of the four test points, I want to see if it already exists in the previous or current time step.
My plan of attack is to generate a 4x2 matrix with the four possible moves, and then search the previous and current set of points to see if it already exists. If it does exist, that point is labeled a 'bad move'. After all four points are tested, I want to "search" for the bad moves, delete them from the possible moves and then randomly select one of the remaining. I've attempted to do this with the following code
for i = 2:time
possible_moves = [1,0;-1,0;0,1;0,-1];
for j = 1:nflies
x = position(j,1,i-1);
y = position(j,2,i-1);
test_points = zeros(4,2);
test_points(:,1) = x;
test_points(:,2) = y;
test_points = test_points + possible_moves;
avail_moves = test_points;
% This is the portion of the code that is not working
for l = 1:4
x_test = test_points(l,1);
y_test = test_points(l,2);
if ismember([x_test,y_test],position(:,:,i-1),'rows')
avail_moves(l,:) = [0.5,0.5];
end
end
bad_moves=find(avail_moves==0.5,1);
avail_moves(bad_moves,:) = [];
next_move = randi(size(avail_moves,1));
position(j,1,i) = avail_moves(next_move,1);
position(j,2,i) = avail_moves(next_move,2);
% This sets the boundary conditions such that as a fly leaves the
% lattice, another one enters at a random point on the opposite
% side
if position(j,1,i) == 0
position(j,1,i) = L-1;
position(j,2,i) = randi(L-1);
elseif position(j,1,i) == L
position(j,1,i) = 1;
position(j,2,i) = randi(L-1);
elseif position(j,2,i) == 0
position(j,1,i) = randi(L-1);
position(j,2,i) = L-1;
elseif position(j,2,i) == L
position(j,1,i) = randi(L-1);
position(j,2,i) = 1;
end
end
plot(position(:,1,i),position(:,2,i),'.k');
axis([0 L 0 L])
frame = getframe;
writeVideo(writerObj,frame);
end
I attempted to delete the bad moves as I check them, but since the loop is for 1:4, if I delete an element, the loop fails. To circumvent this, I tried to label it as [0,0] at first. I would then search the avail_moves string for '0'. Usually this works, but if a potential move is (1,0), say, it sees that zero has a bad move and deletes the element. I tried changing this to label bad moves as [0.5,0.5] since the flies must stay on the lattice points, but this doesn't seem to work either.
I've tried many combinations of ismember and find after reading numerous help threads and the documentation, but I still cannot seem to get it to work.
Thoughts?

Answers (0)

Community Treasure Hunt

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

Start Hunting!