for-loop never reach the times I need

1 view (last 30 days)
Hi,
I have a 130-by-2000 matrix. I need to do two major steps on this matrix. In the first steps, indices of this matrix were randomly selected in a way that only one element in each column was selected. In the second steps, the value of selected index of this 130-by-2000 matrix was randomly changed within a range. The whole procedure need to repeat for 2.1e7 times. I use a for-loop ( for n=1:2.1e7) to repeat 2.1e7 times. I put a tic and toc to count the time every 1e5 times.
The problem is that the time always stops counting on 16700000 times but it still continues simulation till n always ends on 16777216 times. It never reach 2.1e7 times I want.
16777216 is exactly 8^8 or 2^24. WHY?
The memory should be sufficient. I use "whos" to sum the total used memory and it is 5698696 bytes. All variables are in single precision. My laptop is Windows 7 with 8GB memory and CPU is intel Core i7.
Could someone tell me what causes this problem? Why can the for-loop be limited?
Could it be limited by pseudorandom number generating?
Quoted from "Help": The simplest way to generate arrays of random numbers is to use rand, randn, or randi. These functions ALL rely on the SAME stream of uniform random numbers, known as the GLOBAL stream.
Do the amount of pseudorandom numbers in my script already exceed this "global stream" since all rand functions rely on the same stream? I wonder how many numbers can be generated in this global stream?
  3 Comments
Walter Roberson
Walter Roberson on 24 Jun 2012
http://www.mathworks.com/help/techdoc/ref/randstream.list.html
The "period" listed there is the number of random numbers that can be generated before the sequence repeats.
Hsinho
Hsinho on 26 Jun 2012
I still don't know why it stops on 16777216 times. So, I just wrote a second for-loop from 16777217 to 2.1e7 times. Then, I notice that the estimated time by tic/toc showed 3 results with minor difference on the same times, e.g. 16800000 times, since I kept the counting every 1e5 times. The following counting kept showing 3 results on the same times till 2.1e7 times. The estimated time only give one result when n<=16777216.
Could "tic/toc" be the reason causing for-loop stops on 16777216 times? Could it be due to all variables being in single precision that causes 3 tic/toc results on the same times when it is over 2^24 times?

Sign in to comment.

Accepted Answer

Hsinho
Hsinho on 26 Jun 2012
When I resume to use the default double-precision data type of all variables, the tic/toc functions well after 2^24 times. I guess it can finish the for-loop till 2.1e7 times without problems if using double-precision data type.
I have tested it with double-precision data type. Now, the for-loop reaches 2.1e7 times without any problem.
  2 Comments
Walter Roberson
Walter Roberson on 26 Jun 2012
IEEE 754 single precision has only 23 bits stored for the fraction, plus it has 1 "hidden bit", for a total of 24 bits of accuracy. Which corresponds to 2^24.
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
Hsinho
Hsinho on 26 Jun 2012
Thank you! Clear information to know.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!