From: "Anthony Hopf" <>
Newsgroups: comp.soft-sys.matlab
Subject: Randperm, Randi, and Shuffle
Date: Tue, 2 Nov 2010 22:02:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 20
Message-ID: <iaq1os$sak$>
Reply-To: "Anthony Hopf" <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1288735324 29012 (2 Nov 2010 22:02:04 GMT)
NNTP-Posting-Date: Tue, 2 Nov 2010 22:02:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1899184
Xref: comp.soft-sys.matlab:683359

I am trying to replace the following code:

[rix riy riz] = ind2sub(sampbox,randi(myrandistr,prod(sampbox),SPtemp,1));

where sampbox can get large ( [1100         200         200]) and SPtemp can be 125e3.  This is not the case all of the time, but it is definitely bogs down the process.

I want to replace my current code because it seems to show a bias when I plot out the locations it is picking within the 3d space.

Originally I wanted to replace this piece of code with randperm, but it was incredibly slow.  Luckily I found Jan's addition to FEX called Shuttle.  It seemed to really fit the bill because I would specify the number of NOut within his function.  Orders of magnitude faster than randperm for sure.  

Problem is now Shuffle is fast, 0.32sec for the above extreme, but not as fast as randi, 0.014 for the same case.  

This is the code I am using with Shuffle:

        randpts = Shuffle(prod(sampbox),'index',SPtemp);
        [rix riy riz] = ind2sub(sampbox,randpts);

Thank you