Surrounding elements in an array affect and change an element over a loop.
76 ビュー (過去 30 日間)
古いコメントを表示
Okay for some context. Im a uni student, im getting a kick in my rear side. I don't really fully understand what i'm doing in matlabs, so a through explanation if possible would be greatly appreciated.
My task is: The living system is represented as a 2D array of any size, predators, prey and bare ground have the following encoding • Predator → 2 • Prey → 1 • Bare Ground → 0. I need to consider the surrounding elements around any element and if it meets a condition say a predator is in range of a prey, the matrix changes value which is called a generation. This loop occurs for however many generations that the user inputs. If im on the edge of an array, it will loop round and use those elements as the surrounding ones so say a 6x6 matrix where im on Prey(1,6), in the x direction the element surrounding the prey to the right is (1,1). My question is how do I even start a loop which checks every element and sees what surrounds it, I have no idea how to approach this, info im finding online explains the concept on a more basic level of say how loops work, but its not helping.
ngen=7
%G=0
%neighbour=0
%M=[1 2 0;]
M = [0 ,0 ,1 ,0 ,1 ,0 ,1 ,1 ,0 ,0;1 ,1 ,1 ,0 ,0 ,1 ,0 ,0 ,0 ,0;0 ,0 ,0 ,0 ,2 ,0 ,0 ,0 ,0 ,1;1 ,0 ,1 ,0 ,0 ,0 ,1 ,0 ,0 ,0;0 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,1;1 ,0 ,0 ,0 ,0 ,2 ,0 ,1 ,0 ,1;1 ,0 ,1 ,1 ,1 ,1 ,0 ,0 ,1 ,1;0 ,1 ,0 ,1 ,1 ,1 ,2, 1 ,1 ,0;1 ,0 ,1 ,1 ,0 ,1 ,1 ,1 ,1 ,0;0 ,0 ,1 ,1 ,1 ,0 ,0 ,1 ,1 ,0];
[nRow,nCol] = size(M);
if nRow == nCol
else
disp("Invalid square matrice")
M=zeros(1,length(M));
return;
end
for i=1:length(M)
if M(i)~=0 && M(i)~=1 && M(i)~=2
disp('ERROR: wrong inputs')
M=zeros(1,length(M));
return;
end
end
for i=length(M)
if M(i) < 0
M=zeros(1,length(M));
return;
end
end
if ngen <=0
disp("wrong input of generations")
M=zeros(1,length(M));
return;
end
%M=G;
neighbour=zeros(1,3);
%for g=1:ngen
%end
here is my code, the top parts are just testing for some parameter to be met, otherwise I get M=0, but im unsure where to go from here.
回答 (1 件)
Drishan Poovaya
2021 年 11 月 24 日
Hi,
Essentially you want to check all elements in the neighbourhooud of a particualr element but you want the search to wrap around as well. You can do this by some careful application of for loops and using the mod function
% M is a 10x10 array, and for each element we will search a 3x3 grid around
% it
% For example, in the grid below, if the element to be searched is marked
% as x, all elements marked as y will be considered as it's neighbours
% y y 1 0 1 0 1 y y x
% y y 1 0 0 1 0 y y y
% 0 0 0 0 2 0 0 y y y
% 1 0 1 0 0 0 1 0 0 0
% 0 1 0 1 1 0 0 0 0 1
% 1 0 0 0 0 2 0 1 0 1
% 1 0 1 1 1 1 0 0 1 1
% 0 1 0 1 1 1 2 1 1 0
% y y 1 1 0 1 1 y y y
% y y 1 1 1 0 0 y y y
for g=1:ngen
for i = 1:nRow
for j = 1:nRow
%loop over each element
%element is M(i,j)
for ii = i-1:i+1
for jj = j-1:j+1
% Checks neighbours accounting for wrap around
idxI = mod(ii,nRow);
idxJ = mod(jj,nRow);
if idxI == 0
idxI = nRow;
end
if idxJ == 0
idxJ = nRow;
end
%Do something with M(idxI,idxJ)
end
end
end
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!