My script isn't working, can someone please! help me? Further explanation in thread.

1 view (last 30 days)
function M = initializeBoard(N, p)
for i = 1 : N
for j = 1 : N
x = rand(1);
if (x < p)
M(i,j) = 1;
else
M(i,j) = 0;
end
end
end
end
Imagine that this functions creates a N x N matrix A (M).
This matrix consists of 1's and 0's, and the distribution of these depends on p, as can be seen:
if (x < p)
M(i,j) = 1;
else
M(i,j) = 0;
end
function pathFound = findPath(N, p)
A = initializeBoard(N, p);
scanPath = bwlabel(A,4);
[rowNum, ~] = size(scanPath);
stats = regionprops(scanPath, 'PixelList');
pathFound = false;
for iblob = 1:length(stats)
if ((min(stats(iblob).PixelList(:,2)) == 1) && (max(stats(iblob).PixelList(:,2)) == rowNum))
scanPath(scanPath == iblob) = 10*iblob;
pathFound = true;
else
pathFound = false;
end
end
end
This function should check if there's a path between bottom and top of the matrix, and if there is, return a 'true' value.
clear; close all; clc;
pSteps = 20;
M = 100;
N = 200;
probs = [];
ps = [];
for i = 1:pSteps
p = (1/pSteps) * i;
fprintf('p is at %.2f\n', p);
paths = 0;
for m = 1:M
if findPath(N, p) == 1
paths = paths + 1;
end
end
prob = paths / 50;
probs = [probs, prob];
ps = [ps, p];
end
plot(ps, probs)
The script above checks for 20 equally distributed values between 0 and 1 what the probability is of finding a path in a matrix with size N x N.
The output I get from N = 100, M = 100, pStep = 20 (0.05 - 1.00):
loool.png
The output I get from N = 200, M = 100, pStep = 20 (0.05 - 1.00):
aaaakkrzooi.png
However, the output I get from N = 200 and N = 100 is very different from the output I am supposed to get:
pcimg.png
My Question: Does anyone know where I made a mistake / why do my graphs not look like the one shown above at all?
  2 Comments
The Legend
The Legend on 13 Jan 2020
Edited: The Legend on 13 Jan 2020
Uhh, I don't think there are equations?
If p is very small, matrix A will contain only a few 1s. If p is close to 1 almost all elements will be 1. These two situations are the two phases.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!