How can I apply a random number generator in a nested loop?

4 次查看(过去 30 天)
Hi all,
I'm new to matlab, but here is my dilemma: I want to select the cells with the highest values in a matrix (A) within a given range (determined by a radius). The selection of the highest value only happens if a given probability (which considers values t) is high enough. To do this, I am reading in a values (t), and applying this to a probability at every iteration ( for a max of 120 values corresponding to 120 iterations). My results, which is a plot of the cell selected every iteration, is a vertical line across the matrix (which does not make sense). I think it may be an issue with the for loop involving the random number generator. I would appreciate any suggestions concerning how to "fix" this, and I hope my description makes some kind of sense. Below is a shortened version of the code:
for i=1:maxtime % iteration over time steps to the maximum 120 time steps
xstart = xpos-range; % These are start positions
ystart = ypos-range;
for t=1:120; %iteration over all values 1-120
current_t=temp_hourly(t,:); % this updates the value every iteration
prob_thermoreg= current_t-threshold_t; % determines the probability
rn=rand; % generates random number
if prob_thermoreg>rn;
%the logic below selects the cell with the highest value
maxtreecover = 0.0; %find value larger than this
maxx_tree = xstart;
maxy_tree = ystart;
for jj=1:20 %double the radius set at 10 cells
for ii=1:20
if A(ystart + ii,xstart + jj) > maxtreecover; % if the value of A at different ii and jj is greater than max 0.0, select this cell
maxtreecover = A(ystart + ii,xstart + jj);
maxx_tree = xstart + jj;
maxy_tree = ystart + ii;
end
end
end
end
end
%The rest of the code updates the x and y coordinates
end

采纳的回答

Walter Roberson
Walter Roberson 2017-5-3
Your line
current_t=temp_hourly(t,:); % this updates the value every iteration
gives the impression that current_t will be a vector. If so then
prob_thermoreg= current_t-threshold_t; % determines the probability
would be a vector. Then when you did
if prob_thermoreg>rn;
with it being a vector that test would be the equivalent of
if all(prob_thermoreg>rn)
which would fail if any member of prob_thermoreg was less than the random number.
Could you confirm whether current_t will be a vector or a scalar?
  12 个评论
Walter Roberson
Walter Roberson 2017-5-5
I do not see a vertical line as such. I see that most of the change in x and y occurs in the first step, and the rest in the second step, and then no change in x or y. Look at diff(xpossav) and diff(ypossav)
If you put in a disp() or fprintf() after the rand() you will see that the random test is satisfied on most (but not all) t values for every i.
If you look at your code, in each step you are looking for the highest point in A within a certain distance of the current point, and you move there, staying where you are if you are already at the highest point in that range. Then in the next iteration you repeat searching the same distance from where you are currently. Consider now what happens when you reach the highest point in that distance: there is no way to move off of it. So you go to the local peak and you get stuck there.
And that's what you are seeing: you move from the starting point, and you quickly get stuck at a local maxima. (You might even have gotten stuck at the global maxima.)
Stephanie Diaz
Stephanie Diaz 2017-5-5
I see exactly what you are saying, thank you! I will add some sort of if statement to prevent this, perhaps increasing the distance for selecting the maximum point.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by