For-loop positive integer error and For-loop construction

2 views (last 30 days)
I am attempting to merge this script:
x = 576; % line length
a = 220; % position of marker
edge1 = 0; % left edge
edge2 = x; % right edge
sample = 1000000; % number of samples to generate
lambda = 3:4; % range of λ values for poisson distributions
rnddist = round(poissrnd(repmat(lambda,sample,1))); % Generates psuedo-random numbers based on poisson distribution probabilities
len = zeros(size(rnddist)); % zeroed array to record sampling results
s = 1:(size(len,2)); % calculates number of columns within len and creates an array
for n = s; % For each distribution curve
for i = 1:sample % For each round of sampling
numpoint = rnddist(i,n);
p = round(rand(numpoint,1) * x); % generate 'numpoint' number of random points
len(i,n) = min([p(p>a);edge2]) - max([p(p<a);edge1]); % Calculates and records fragment sizes
end
end
tally = histc(len,unique(len)); % Tallies the instances of each fragment size into a new array
Which creates a 'line' in the form of a 1D-matrix and positions a marker a certain distance along it. It then subsequently generates a set of random numbers that correspond to points on the line. The amount of numbers generated with each round is determined by a poisson distribution (poissrnd). It then calculates the smallest distance between any 2 generated points where the interval between them still contains the marker.
With this script:
x = zeros([1 110]);
x(6:105)=1;
dist = (abs(-5:5).^2)/25;
nsamples = 3;
for n = 1:nsamples
p(1,n)= datasample(1:110,1,'Weights',x);
x(p-5:p+5)=x(p-5:p+5).*dist;
end
Which choose points again on a line but they are no longer fully random as they are influenced by the positions of other points which create 'zones of exclusion' around them, lowering the probability that a point within the zone is chosen, helping to space the points out.
In essence, i'd like the first script to gain this ability such that for each point it makes, this point influences the positions of those following it - but keeping the output the same i.e. the size of the smallest distance between any two points still containing the marker.
I have tried to craft the for-loop to do this but I simply do not know the answer nor the most efficient way to do this. The script in my original post, which showed how far I had got, was:
x = zeros([1 110]);
x(10:100)=1;
dist = (abs(-10:10).^2)/100;
lambda = 3:4;
sample = 1000000;
rnddist = round(poissrnd(repmat(lambda,sample,1))); % Generates psuedo-random numbers based on poisson distribution probabilities
len = zeros(size(rnddist)); % zeroed array to record sampling results
s = 1:(size(len,2)); % calculates number of columns within len and creates an array
for i = 1:sample
for n = s
cuts = rnddist(i,n);
cutpos(cuts,i) = datasample(1:110,1,'Weights',x);
x(cutpos(cuts)-10:cutpos(cuts)+10)=x(cutpos(cuts)-10:cutpos(cuts)+10).*dist;
end
end
This returns the error - "Subscript indices must either be real positive integers or logicals." I suspect I need some way in which to properly index 'x' but my skills in MATLAB scripting are rather limited as I am still learning so I do not have many ideas left.
Would anybody be able to offer any suggestions or code itself?
Anna.

Accepted Answer

Image Analyst
Image Analyst on 17 Sep 2014
For some reason, you chose to snip out a small part of the error message and not tell us the critical part, and that is which line of code causes it. Chances are that cuts or cutpos(cuts)is some fraction number, not an integer starting at 1, like 1,2,3,4,5,etc. Try putting these lines before the x line
cuts
cutpos
  2 Comments
Anna
Anna on 17 Sep 2014
dbstop if error
x(cutpos(cuts)-10:cutpos(cuts)+10)=x(cutpos(cuts)-10:cutpos(cuts)+10).*dist;
Is the line which it stops.
Adding cuts and cutpos returns:
cuts =
3
cutpos =
0
0
16
cuts =
4
cutpos =
0
0
16
64
cuts =
4
cutpos =
0 0
0 0
16 0
64 32
cuts =
7
cutpos =
0 0
0 0
16 0
64 32
0 0
0 0
0 80
Subscript indices must either be real positive integers or logicals.
I am quite puzzled as to why 0's are appearing in cutpos when the edges of the 'x' are zeroed out to prevent any numbers being chosen there.
Iain
Iain on 17 Sep 2014
Ok, the error is that you're mixing indexing types.
There's 3 types of indexing.
1. n-dimensional. This is: a(b,c,d,e, ...)
2. Logical. This is a(a>5)
3. 1-dimensional. this is a(5);
You're setting cutpos like this "cutpos(cuts,i)"
You're accessing cutpos like this "cutpos(cuts)"
So, when you set, cutpos(7,2), you're then looking for cutpos(7), which is cutpos(7,1). This then results in a 0, and that causes your error.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!