towards object oriented programming?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!