From: Jan Simon on 12 Jul 2010 06:52 Dear Craig, > for i=1:100 > ord=0; % *** delete this line *** > ord=randperm(66); > arp=a(ord); > bar=arp(1:x); > result=var(bar); > end Look in the source of RANDPERM to get a matrix of sorted random indices. You could try SHUFFLE from the FEX as a faster replacement of RANDPERM. But for such small vectors as 66 the difference cannot be large. > for k=1:50 > for h=1:50 > counting=0; > for j=1:76 > if R(j,3)== k > y(j,h,k) = x(j); > counting=counting+1; > elseif R(j,3) == h && h~=k > Y(j,h,k) = x(j); > counting=counting+1; > end > end > end Move the FOR loop over j into the variables: for k=1:50 for h=1:50 match = or(R(1:76, 3) == k, R(1:76, 3) == h); y(1:76, h, k) = x; counting = sum(match); end end If R and y have the desired sizes, write ":" instead of the slower "1:76". Good luck, Jan
From: Steven Lord on 12 Jul 2010 09:55 "Craig " <physicsmathworks(a)gmail.com> wrote in message news:i1dt13$p37$1(a)fred.mathworks.com... > Hi all. I'm just coming to terms with the way MATLAB processes things, and > am quickly realising the benefits in the avoidance of looping wherever > possible.. > I have two set ups I would be ever grateful for assistance with, if > possible. I know questions like this are asked all the time, I would also > be delighted if someone could point me to a resource to help with this > matter. > > First, I have a simple thing: > for i=1:100 > ord=0; > ord=randperm(66); > arp=a(ord); > bar=arp(1:x); > result=var(bar); > end When you post examples like this, please include a prose explanation of what you intend the code to do / think the code does. For instance, it looks like you're _almost_ doing what BOOTSTRP from Statistics Toolbox does: http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bootstrp.html except the documentation for BOOTSTRP indicates it samples with replacement while your code does not. If your application would work if you allowed sampling with replacement, and you have access to Statistics Toolbox, try replacing this code with BOOTSTRP. > Second, I have what I am positive is a horrible set up: > for k=1:50 > for h=1:50 > counting=0; > for j=1:76 if R(j,3)== k > y(j,h,k) = x(j); counting=counting+1; > elseif R(j,3) == h && h~=k > Y(j,h,k) = x(j); > counting=counting+1; > end > end > end Again, an explanation of what this code is intended to do would be helpful in determining if there are built-in functions that do some or all of what you want to accomplish. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: Walter Roberson on 12 Jul 2010 11:17 Craig wrote: > 'a' is simply a vector that is re-arranged according to the randperm. > 'var' is the MATLAB function to calculate the 'variance'. > > I have missed out a part of the above code. It should read: > > result(i) = var(bar); > So that 'result' ends up being a vector storing a value of variance for > each pass of the loop. > I believe 'var' does work with matrices. Is it then possible in some > fashion to set up a matrix with all of the combinations of 'a' that i > want to look at and calculate the variance of each of them in 'one go' > as it were? Yes, var(bar) calculates variances along columns of bar. You can also use var(bar,2) to calculate variances along rows of bar. Internally, randperm() is a simple but very elegant function, [unused, Permutation] = sort(rand(1,N)) You can use the same idea, nruns = 100; [unused, Permutations] = sort(rand(nruns,La),2); arp = a(Permutations); result = var(arp(:,1:x),2); I have not tried the above to be certain that I got the rows vs columns correct.
From: Craig on 12 Jul 2010 11:27
Thank you to everyone who has replied so far. and Hi Steven, Thank you for your reply - of course you are right, I should have been much clearer! > > First, I have a simple thing: > > for i=1:100 > > ord=0; > > ord=randperm(66); > > arp=a(ord); > > bar=arp(1:x); > > result=var(bar); > > end > > When you post examples like this, please include a prose explanation of what > you intend the code to do / think the code does. For instance, it looks > like you're _almost_ doing what BOOTSTRP from Statistics Toolbox does: > > http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bootstrp.html > > except the documentation for BOOTSTRP indicates it samples with replacement > while your code does not. If your application would work if you allowed > sampling with replacement, and you have access to Statistics Toolbox, try > replacing this code with BOOTSTRP. > I will certainly take a look at BOOTSTRP. Thanks for the suggestion. As for what I want the code to do: I have some results from an assay experiment that have been recorded in a particular order. I was hoping to randomize this order many times so that I can check to see how often the results I have obtained might come up in a random experiment. The variance (I am told) is important since I am basically looking at many experimental results in one vector. The variance then covers each of the individual experiments within this vector. I am relying on my mathematician for this part. > > Second, I have what I am positive is a horrible set up: > > for k=1:50 > > for h=1:50 > > counting=0; > > for j=1:76 if R(j,3)== k > > y(j,h,k) = x(j); counting=counting+1; > > elseif R(j,3) == h && h~=k > > Y(j,h,k) = x(j); > > counting=counting+1; > > end > > end > > end > > Again, an explanation of what this code is intended to do would be helpful > in determining if there are built-in functions that do some or all of what > you want to accomplish. > This leads on from my previous explanation though it is a slightly different experiment. I have 50 different assay results, but they are part of only 7 different sections. (the 'for' should actually cover j=1:7, sorry for the error). So, I want to group my assay results in pairs. I have then run over each of them twice, so that I can have 1 paired with 2, and 1 paired with 3 etc etc as well as 2 paired with 3, 2 paired with 4 and so on. One of the problems I am having is narrowing it down - having two for 1:50 means that I am (i think) taking permutations as unique. That is, [1 2] is different from [2 1] - however, obviously it is not. this means I am needlessly doing each calculation twice. Thank you, Craig |