Evaluate neighboring nodes in a 2D lattice and change node value, as required
Show older comments
Hello All,
Thanks to some excellent help from you, I have the code I need to create a 2-D lattice with nodes assigned random values of 1 or 2, plotted by color:
if true
% Generate the random values on the nxn, 2-dimensional lattice
% using X = randi(imax,sz1,...,szN) syntax.
n = 100;
P = randi(2, n+1, n+1); % 0:n -> n+1 elements
% Use linear indexing to find the values to plot.
isOne = P==1;
[i1,j1] = ind2sub(size(P),find( isOne)); % ones
[i2,j2] = ind2sub(size(P),find(~isOne)); % twos
% Create figure with axes on hold, so successive plot commands will add and
% not replace. Also add zero-based axis limits and make the axes square.
figure
axes('NextPlot','add','XLim',[0 size(P,1)-1],'YLim',[0 size(P,2)-1]);
axis square;
% Plot the figure. Subtract 1 to get zero-based ids.
plot(i1-1, j1-1, 'bo', 'MarkerFaceColor', 'b');
plot(i2-1, j2-1, 'ro', 'MarkerFaceColor', 'r');
end
The next step is to evaluate the 4 neighboring nodes: above, below, left, right (not diagonal). For the nodes at the corners and edges, they would only evaluate the 2 or 3 neighbors that they do have. If two or more of the neighboring nodes have a different value (eg, if the middle node = 1 and at least two of the neighboring nodes = 2, then the middle node will change its value to equal the prevailing "opinion." I need to iterate this process for a number of time steps (say 100) to observe the clustering effect.
The code I have so far doesn't work for the edge and corner nodes and I'm not sure it handles the nodes above and below, either.
if true
% Run the time steps to evaluate neighboring nodes and increment the
% counter if values are not equal. Change or retain value based on
% prevailing "opinion."
timeStep = 100;
count = 0;
for iteration = 1:timeStep
for node = 1:n+1
if [i1(node), j1(node)] ~= [i1(node-1), j1(node-1)];
count = count+1;
else count = count;
end
if [i1(node), j1(node)] ~= [i1(node+1), j1(node+1)];
count = count+1;
else count = count;
end
if [i2(node), j2(node)] ~= [i2(node-1), j2(node-1)];
count = count+1;
else count = count;
end
if [i2(node), j2(node)] ~= [i2(node+1), j2(node+1)];
count = count+1;
else count = count;
end
if count >= 2 && [i1(node),j1(node)] == 1;
[i1(node),j1(node)] == 2;
elseif count >= 2 && [i1(node),j1(node)] == 2;
[i1(node),j1(node)] == 1;
end
if count >= 2 && [i2(node),j2(node)] == 1;
[i2(node),j2(node)] == 2;
elseif count >= 2 && [i2(node),j2(node)] == 2;
[i2(node),j2(node)] == 1;
end
end
end
% Repeat the steps above to find and plot correct values
% Use linear indexing to find the values to plot.
isOne = P==1;
[i1,j1] = ind2sub(size(P),find( isOne)); % ones
[i2,j2] = ind2sub(size(P),find(~isOne)); % twos
% Create figure with axes on hold, so successive plot commands will add and
% not replace. Also add zero-based axis limits and make the axes square.
figure
axes('NextPlot','add','XLim',[0 size(P,1)-1],'YLim',[0 size(P,2)-1]);
axis square;
% Plot the figure. Subtract 1 to get zero-based ids.
plot(i1-1, j1-1, 'bo', 'MarkerFaceColor', 'b');
plot(i2-1, j2-1, 'ro', 'MarkerFaceColor', 'r');
end
If you have any ideas, I would greatly appreciate your help!
Accepted Answer
More Answers (0)
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!