towards object oriented programming?

2 ビュー (過去 30 日間)
cgo
cgo 2015 年 11 月 2 日
コメント済み: cgo 2015 年 11 月 4 日
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.

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 11 月 2 日
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 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 2 日
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 2015 年 11 月 4 日
Thanks for your help. The restrictions are different per particle.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by