randi function halt my program from execution sometimes...

5 次查看(过去 30 天)
Hello, does anyone had this kind of problem described in title with randi function.
ps. i also get the same issue with randsample (maybe i ve got a programming issue and both work fine?)
Appreciated !!!
  1 个评论
John D'Errico
John D'Errico 2016-11-15
编辑:John D'Errico 2016-11-15
How can we know what you are doing wrong? The crystal ball is so foggy these days. Maybe I need a new one, or perhaps I can order some crystal ball cleaner online.
Those tools work with no problems for everyone else. So if it causes a problem for you, then you need to post an example that causes a problem. Show the values of the inputs you passed in. How else can we possibly know what you have done wrong?
Would you call your doctor and tell them only that there is a problem with your foot, but give them no more hint at all? Then would you seriously expect them to diagnose your problem over the telephone with only that input? Would you call your mechanic and tell them only that on some days, your car does not run, then expect good advice based on only that input?

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2016-11-15
The documentation says:
X = randi(imax,n) returns an n-by-n matrix of pseudorandom integers drawn from the discrete uniform distribution on the interval [1,imax].
Are you perhaps doing that and passing in a large value of n? This will create a 2-D matrix. If n is very large, like a million, then that could take a long time. Perhaps you wanted a 1-D vector of n elements. If that is the case, do this:
r = randi(imax, n, 1); % Column vector
or
r = randi(imax, 1, n); % Row vector

Jan
Jan 2016-11-15
Both work fine. To check, if you have a programming issue, post your code. A frequent mistake is using a larg single input only:
a = randi([0,1], 1e6)
This returns a 1e6x1e6 matrix, which exhausts the memory and let Matlab look like it is frozen. Usually randi([0,1], 1, 1e6) is meant.

stelios krasadakis
stelios krasadakis 2016-11-15
Hello and thanks for your answers.
I wanted to know if anyone experienced the same freeze like me in order to avoid possible wasting time for the others. I haven't posted any code before because i really appreciate your time..
So, here is the thing (below you can see a part of GA and more specific from mutation operation).
% Select Parent population
i=randi([1 nPop]); %nPop value 100 <-Freezes Here
indiv=pop(i);
% Apply Mutation
nVar=numel(indiv.GreenNSWE); % indiv.GreenNSWE struct with int array[four elements] and int variable
nmu=ceil(mutation*nVar); %mutation=mutation percentage , Var(four elements from int array)
j=randi([1 Var]); % <-Freezes Here
sigma=0.01*(greenMax-greenMin);
indiv.GreenNSWE(j)=indiv.GreenNSWE(j)+sigma;
Really appreciated,
Best Regards
Stelios
  6 个评论
Image Analyst
Image Analyst 2016-11-15
Are you sure you're not piling stuff up in an axes, like tons of images. This may slow it down quite a bit. You might want to call
cla reset;
before you display anything.
stelios krasadakis
stelios krasadakis 2016-11-15
Yes i'm sure...
Nevermind, i ll try to fix this problem and i ll let community know.
Thank you all of the above for your attention !

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by