towards object oriented programming?

3 views (last 30 days)
cgo
cgo on 2 Nov 2015
Commented: cgo on 4 Nov 2015
I have n particles which I want to study individually. At first, they are randomly distributed over a certain region so I can create a nx2 matrix to denote the position of each particle. Next, it becomes more complicated: I am studying which of these particles are within the radius of a certain predetermined position. So, I now move these particles away in a certain direction governed by physics. Hence, SOME of the entries of the nx2 matrix change. We repeat this procedure by selecting different locations of interest. So the entries of the matrix keep on changing.
Now I am not asking you to write me a code, but I couldn't think of an intuitive plan on how to 'micro-manage' each particle so that it is easy to analyze them one by one. Is this problem addressed by 'object oriented programming.'? I am totally unfamiliar with it. So I want to know your insights on what could be the best way to address this problem.

Answers (1)

Walter Roberson
Walter Roberson on 2 Nov 2015
Use logical indexing.
Let Positions be your n x 2 matrix, and Target be an 1 x 2 vector to be compared against.
dists = sqrt( sum(bsxfun(@minus, Positions,Target).^2,2) );
isinrange = dists < R;
Now isinrange is a logical vector, one element per row, that indicates whether the row is to be selected. So move only those. For example if they get moved by the constant shift [deltax, deltay] then
Positions(isinrange,:) = bsxfun(@plus, Positions(isinrange,:), [deltax, deltay]);
  3 Comments
Walter Roberson
Walter Roberson on 2 Nov 2015
Yes, this is usually quite feasible even when n is large.
I used a fixed translation as one code example.
Is the restriction the same for all particles or is the restriction different for each particle? If it is different for each particle, then can the restriction be described by matrices (or vectors), that can be indexed by row to determine the per-particle restriction?
cgo
cgo on 4 Nov 2015
Thanks for your help. The restrictions are different per particle.

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!