How can I check adjacent matrix values for a value?
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!