1

I have a matrix of zeros and ones. Each cell with a one value represents a non empty cell. The cells with the ones is kept in a vector with the coordinates. I'm iterating through the list and move each element randomly to one of its free neighboring cells.

Is there a way to do this with vector operations?

Thanks

bla
  • 25,846
  • 10
  • 70
  • 101
Guy Wald
  • 599
  • 1
  • 10
  • 25
  • Can dots move on top one another, or is there repulsion? Vector operations means that you need to be able to move in parallel. – Jonas Jan 18 '13 at 19:07
  • They can't be on top one another. Only one dot per cell :| – Guy Wald Jan 18 '13 at 19:15
  • since this can be implemented in a 1-D loop, I would assume that a for loop would be faster than a "vectorized" arrayfun. – bla Jan 18 '13 at 19:36
  • 1
    Have you profiled your code to identify where the bottleneck is? Such a small problem should be pretty fast, as long as you only represent the points by their coordinates, instead of attempting to create the zero and one matrix at each iteration (which is completely unnecessary, and which can be a vectorized operation at the end of the simulation) – Jonas Jan 18 '13 at 19:48

2 Answers2

1

Here's an attempt, not the most elegant or efficient one, but still it should work.

first I assume you have the x,y coordinates of your non empty cells, something like

c=[x y];

The relative positions of the 8 nearest neighbors (n.n.) in a 2D array are given by:

nn=[1 1;1 -1;-1 1;0 -1;-1 0;0 1;1 0;-1 -1];

Let's take all possible permutation of each x,y coordinate in c around its n.n.

permc=bsxfun(@plus,repmat(c,[1 8]),nn(:)');

Now set a vector of random n.n. out of the 8 options for each x,y coordinates:

ri=randi(8,numel(x), 1);

and use it to select random new coordinates

new_c= [ permc(sub2ind(size(permc), (1:numel(x))', 2*ri-1 )) , ...
         permc(sub2ind(size(permc), (1:numel(x))', 2*ri))];  

Issues:

  • The code doesn't check if there is a n.n. that is not free, not hard to solve having info of c and permc at hand.

  • The code doesn't care that a point is on the edge of the array, so it can select a coordinate that is either 0 or one larger than the size of the array. Again, permc has the info to tackle this case and it can be treated separately in several ways.

That's the jist.

bla
  • 25,846
  • 10
  • 70
  • 101
0

Probably, but I think it would be quite tricky to write. You'd have to be careful to prevent two "dots" from moving into the same empty location at the same time.

Looping over the vector and dealing with one dot at a time seems far easier. What's your reason for wanting to do this with vector operations? If you're looking to make your code faster, post your code and ask for suggestions.

shoelzer
  • 10,648
  • 2
  • 28
  • 49
  • Speed. If i have a 10,000 cell matrix filled with 5000 dots it's very slow on each complete iteration of the list. – Guy Wald Jan 18 '13 at 19:16
  • 1
    Sounds like a great candidate for a `mex` extension to me. Should be very easy to write as a straight loop in C. – japreiss Jan 18 '13 at 19:18
  • 1
    Remember that Matlab has a JIT for speeding up for loops. The loops should therefore not be as feared as they once were. See my answer here for more details: http://stackoverflow.com/questions/14035365/why-does-vectorized-code-run-faster-than-for-loops-in-matlab/14128983#14128983 – KlausCPH Jan 18 '13 at 20:18