towards object oriented programming?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Walter Roberson
el 2 de Nov. de 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 comentarios
Walter Roberson
el 2 de Nov. de 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?
Ver también
Categorías
Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!