Setting up parasite model

9 ビュー (過去 30 日間)
Patrick Carey
Patrick Carey 2018 年 10 月 10 日
回答済み: Star Strider 2018 年 10 月 10 日
I am trying to set up a parasite model that represents a grid of 200*200 cells.
In this grid there has to be 20 parasites and 20 food at random locations. I used this code:
fxPos=randi(200,20,1); fyPos=randi(200,20,1);
food=[fxPos fyPos];
pxPos=randi(200,20,1); pyPos=randi(200,20,1); pLifespan=randi(15,20,1); parasites=[pxPos pyPos pLifespan];
What I am stuck on is how can I make certain that no two particles end up on the same position on the grid.

回答 (1 件)

Star Strider
Star Strider 2018 年 10 月 10 日
One way you can check to be certain that no two positions are the same is to be certain that the positions are unique:
foodRows = 0;
while foodRows < 20
fxPos=randi(200,20,1);
fyPos=randi(200,20,1);
food=[fxPos fyPos];
[Ufood,~,ix] = unique(food, 'rows');
foodRows = size(Ufood,1)
end
That may be the easiest (and likely most efficient) option.

カテゴリ

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