towards object oriented programming?

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.

Antworten (1)

Walter Roberson
Walter Roberson am 2 Nov. 2015

1 Stimme

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 Kommentare

cgo
cgo am 2 Nov. 2015
Thanks for your insight. Is this way feasible/sustainable such that we can even when n is large, we can micromanage/analyze each particle? I forgot to mention that the particles will move at the same time in one time step, but they do not move with the same deltax,deltay as with all the rest. In short, each particle moves a specific amount according to some restriction. But not all particles move. Thanks for your help.
Walter Roberson
Walter Roberson am 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 am 4 Nov. 2015
Thanks for your help. The restrictions are different per particle.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Hilfe-Center und File Exchange

Gefragt:

cgo
am 2 Nov. 2015

Kommentiert:

cgo
am 4 Nov. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by