randi function halt my program from execution sometimes...

15 views (last 30 days)
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 Comment
John D'Errico
John D'Errico on 15 Nov 2016
Edited: John D'Errico on 15 Nov 2016
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?

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 15 Nov 2016
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 on 15 Nov 2016
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 on 15 Nov 2016
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 Comments
Image Analyst
Image Analyst on 15 Nov 2016
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 on 15 Nov 2016
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 !

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!