How can I check adjacent matrix values for a value?

12 visualizaciones (últimos 30 días)
Heather Breidenbach
Heather Breidenbach el 7 de Nov. de 2020
Comentada: Matt J el 7 de Nov. de 2020
function e=evosim()
board=(zeros(12,12));
pop=1;
charac=([1, 0]);
cloc=[];
for i=1:pop %%Population generator%%
popcount=1;
a=randi([1,12]);
b=randi([1,12]);
if board(a,b)==0
board(a,b)=charac(1);
cloc=[b a popcount; cloc]
end
end
for i=1:10 %%Food generator%%
a=randi([1,12]);
b=randi([1,12]);
if board(a,b)==0
board(a,b)=2;
end
end
e=board;
for i=1:pop
xcord=cloc(i,1);
ycord=cloc(i,2);
if board(xcord-1,ycord)==2
end
Here's my code so far: basically, I have a blank 12x12 matrix, and randomly place 1's and 2's. I want to check the 8 squares around the 1, and the x and y coordinates of any 1s are stored in cloc. After it checks, I want the 1 to 'jump' to the location of where the 2 was.
I think I could do it by checking for (xcord+-1, ycord+-1,) etc, and recording the coordinates of the matrix 1s. However, I was wondering if there was a more efficient way of doing this.
I am new to matlab as well, so any suggestions are more than welcome in improving my code! Thanks!

Respuesta aceptada

Matt J
Matt J el 7 de Nov. de 2020
I don't understand the rules of evolution of the board that you described. However, an efficient way to gather all the 8-neighbor groups for subsequent analysis is as follows. Here, I've used a 4x4 board size for illustration,
n=4; %board size
[I,J,dI,dJ]=ndgrid(1:n,1:n,-1:1,-1:1);
I=reshape(I+dI,[],9); clear dI
J=reshape(J+dJ,[],9); clear dJ
valid=(I>=1 & I<=n) & (J>=1 & J<=n);
I(~valid)=nan;
J(~valid)=nan;
lookup=sub2ind([n,n],I,J);
lookup(~valid)=1;
lookup(:,5)=[];
valid(:,5)=[];
mask=double(valid); mask(~valid)=nan;
The key results of this are lookup and mask. You would compute them only once and pass them to your function for repeated use. You use them as follows, to form a matrix whose rows are the neighbors of successive locations on the board. The NaNs indicate nieghbors outside the boundaries of the board.
board=rand(n),
board = 4×4
0.9145 0.9106 0.0653 0.7765 0.3478 0.6923 0.7918 0.1343 0.9213 0.8086 0.7676 0.8892 0.1573 0.2661 0.0432 0.7342
neighbors = board(lookup).*mask
neighbors = 16×8
NaN NaN NaN NaN 0.3478 NaN 0.9106 0.6923 NaN NaN NaN 0.9145 0.9213 0.9106 0.6923 0.8086 NaN NaN NaN 0.3478 0.1573 0.6923 0.8086 0.2661 NaN NaN NaN 0.9213 NaN 0.8086 0.2661 NaN NaN 0.9145 0.3478 NaN 0.6923 NaN 0.0653 0.7918 0.9145 0.3478 0.9213 0.9106 0.8086 0.0653 0.7918 0.7676 0.3478 0.9213 0.1573 0.6923 0.2661 0.7918 0.7676 0.0432 0.9213 0.1573 NaN 0.8086 NaN 0.7676 0.0432 NaN NaN 0.9106 0.6923 NaN 0.7918 NaN 0.7765 0.1343 0.9106 0.6923 0.8086 0.0653 0.7676 0.7765 0.1343 0.8892
  2 comentarios
Heather Breidenbach
Heather Breidenbach el 7 de Nov. de 2020
Thanks so much! I haven't finished writing a lot of it so there's not really an evolution part yet. This really was helpful!
Matt J
Matt J el 7 de Nov. de 2020
You're quite welcome. Please Accept-click the answer if and when you decide it gives you what you need.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Oil, Gas & Petrochemical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by