Plotting multiple data sets onto the same graph - Complex Study

2 views (last 30 days)
I am trying to plot multiple data sets onto the same graph. The complete code is given below to run the model so hopefully it is clear what I am trying to do. Any help would be greatly appreciated as I am trying to push forwards with this project.
If someone could either help re-write this code or explain how to implement the hold and hold off commands to do this (Ive never worked with code as complicated as this before) it would be greatly appreciated
Kind regards and many, many thanks John
_______________________________________________________________________
Firstly the Neighbours function (save this as a function):
function nbrs = neighbours(m, n, bdry, conn, prnt)
% function nbrs = neighbours(m, n, bdry, conn)
%
% calculate adjacency list for image graph
%
% m, n = image dimensions (m x n)
% bdry = boundary condition 'closed' or 'torus'
% conn = connectivity '4' or '8'
% prnt = optional printout (print when prnt = 1)
%
% nbrs = cell array of lists of indices k = 1 to m*n
% nbrs{k} = [k1 k2 k3 ...]
% Lookup table for i, j values of the three
% [-1 0 1]-shifted versions of rows and coumns,
% NaN indicates neighbour is off edge
switch bdry
case 'closed'
I = [[NaN 1:m-1]; 1:m; [2:m NaN]];
J = [[NaN 1:n-1]; 1:n; [2:n NaN]];
case 'torus'
I = [[m 1:m-1]; 1:m; [2:m 1]];
J = [[n 1:n-1]; 1:n; [2:n 1]];
end
%%%%nested function to simplify coding below
function k = ind(i1, j1, i, j)
k = sub2ind([m n], I(i1, i), J(j1, j));
end
nbrs = {};
for k = 1:m*n
[i j] = ind2sub([m n], k);
switch conn
case '4' % fancy formatting so easy to check layout
nbr = [ind(1, 2, i, j) ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 2, i, j)];
case '8'
nbr = [ind(1, 1, i, j), ind(1, 2, i, j), ind(1, 3, i, j), ...
ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 1, i, j), ind(3, 2, i, j), ind(3, 3, i, j)];
end
nbrs{k} = nbr(~isnan(nbr)); % remove any missing (boundary) pixels
end
if prnt == 1
for k = 1:length(nbrs)
fprintf('%5d :', k); fprintf('%5d ', nbrs{k}); fprintf('\n')
end
end
end % function end
___________________________________________________________________
Here is the code for the function rand_binary
function im = rand_binary(m, n, prob)
% function im = rand_binary(prob)
%
% Make binary image with pixels randomly set to black
%
% m, n = image size (m x n)
% prob = probablity of black pixel
%
% im = returned image
im = double(rand(m, n) > prob);
__________________________________________________________________
________________________________________________________________
And run_simulation function:
function [pop2, xagg] = run_simulation(pop1, sf, nbrs, niter, plt)
% function [pop2, xagg] = run_simulation(pop1, sf, nbrs, niter, sf, plt)
%
% pop1 = binary image for initial aggression map(aggressive = 0 = black)
% sf = binary image for short-fuse map (short-fuse = 0 = red-star)
% nbrs = adjacency list to specify network topology
% niter = number of updates to apply
% plt = plot flag (0 = no plots, 1 = ahow plots)
%
% pop2 = final aggresion map
% xagg = final proportion of aggressives
if nargin < 5
plt = 1;
end
[m n] = size(pop1);
if plt
plot_world(pop1, sf);
drawnow
end
a = 0; % aggressive
p = 1; % peaceful
pop2 = pop1;
for i = 1:niter
for k = 1:m*n
nbr = nbrs{k};
na = sum(pop1(nbr) == a); % number aggressive;
np = sum(pop1(nbr) == p); % number peaceful
if sf(k) == 0
if na >= 1
pop2(k) = a;
else
pop2(k) = p;
end
else
if na > np
pop2(k) = a;
elseif na < np
pop2(k) = p;
end % else stays unchanged
end
end
pop1 = pop2;
if plt
plot_world(pop1, sf);
title(sprintf('Day %d', i+1));
pause(0.2)
end
end
xagg = sum(pop2(:) == a)/(m*n);
____________________________________________________________________
Now this is the code to plot my graph. If you run this data as it is it should plot a smooth sigmoidal curve for a single sf value. However I want to compare the effect of different sf values by plotting their results on the same graph.
clear all % check this is stand-alone
% specify grid size and topology
m = 100;
n = 100;
nbrs = neighbours(m, n, 'closed', '4', 0);
% independent variable: proportion of aggressives
nsamp = 1000; % number of values of pagg to use
pagg = linspace(0, 1, nsamp); % pagg increases from 0 (none aggaggresive) to 1 (all aggresive)
% dependent variable xagg: the final proportion of aggresssives
xagg = NaN(1, nsamp); % xagg is storage for this number, one for each pagg value
% specify proportion of short-fuse individuals
psf = 0.2;
sf = rand_binary(m, n, psf);
% number of iterations
ndays = 20;
niter = ndays-1;
plt = 0; % no movie
figure(3); clf
for i = 1:nsamp
pop1 = rand_binary(m, n, pagg(i)); % make initial population with pagg(i)
[pop2, xagg(i)] = run_simulation(pop1, sf, nbrs, niter, plt); % get final population & proportion of aggressives
figure(3);
plot(pagg, xagg, '.', 'MarkerSize', 10)
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
drawnow
end
Any help with this problem would be greatly appreciated
Many thanks again, John

Answers (1)

Mischa Kim
Mischa Kim on 11 May 2015
Edited: Mischa Kim on 11 May 2015
John, add
hold all
or
hold on
before entering the for loop. Consequently, you should also add a
hold off
after the end of the loop.
  1 Comment
John Doe
John Doe on 11 May 2015
After adding the hold on and hold off, Would it I just be repeating the above code but adjusting the sf value to be sf1? Struggling to get my head around this.
Thanks for the reply, John

Sign in to comment.

Categories

Find more on Printing and Saving 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!