f



speed up for loop inside for loop short question

Hi there,

Given three column vectors x,y,z I want to calculate the probabilities: 

P(X<Y<Z), P(X=Y<Z), P(X<Y=Z), P(X=Y=Z).

The following code gets the job done but I would like it to be faster. Is there a way of getting rid of at least one of the for loops?

(I described the problem above instead of just giving the code because it might be helpful for someone to take a completely different approach, or check me (I think this is correct). Anyway if you are bored to check, any ideas regarding the speed would be more than welcome!).

Thanx in advance for any answers!

op=1;
for i=1:length(x)
    for j=1:length(y)
        xx=x(i).*ones(length(z),1);
        yy=y(j).*ones(length(z),1);
        k1(op)=length(find(xx<yy & yy<z));
        k2(op)=length(find(xx<yy & yy==z));
        k3(op)=length(find(xx==yy & yy<z));
        k4(op)=length(find(xx==yy & yy==z));
        op=op+1;
    end
end
op=op-1;
nx=length(x);ny=length(y);nz=length(z);
pith1=sum(k1)./(nx*ny*nz)  %P(X<Y<Z)
pith2=sum(k2)./(nx*ny*nz)  %P(X<Y=Z)
pith3=sum(k3)./(nx*ny*nz)  %P(X=Y<Z)
pith4=sum(k4)./(nx*ny*nz)  %P(X=Y=Z)
0
bleonidas25 (182)
7/30/2011 4:15:13 PM
comp.soft-sys.matlab 211266 articles. 15 followers. lunamoonmoon (258) is leader. Post Follow

4 Replies
751 Views

Similar Articles

[PageSpeed] 3

I think I found my way out!

nx=length(x);
ny=length(y);
nz=length(z);


X=reshape(repmat(x,[1 ny*nz])',1,numel(repmat(x,[1 ny*nz])))';
Y=reshape(repmat(y,[nx nz])',1,numel(repmat(y,[nx nz])))';
Z=repmat(z,[nx*ny,1]);

n=length(X);
pith1=length(find(X<Y  & Y<Z ))./n;
pith2=length(find(X==Y & Y<Z ))./n;
pith3=length(find(X<Y  & Y==Z))./n;
pith4=length(find(X==Y & Y==Z))./n;


Is the above correct?
0
bleonidas25 (182)
7/30/2011 5:35:34 PM
"leo nidas" <bleonidas25@yahoo.gr> wrote in message <j11amh$o12$1@newscl02ah.mathworks.com>...
> Given three column vectors x,y,z I want to calculate the probabilities: 
> P(X<Y<Z), P(X=Y<Z), P(X<Y=Z), P(X=Y=Z).
> ........
- - - - - - - - - - -
  I claim that with large nx, ny, and nz, if you initially sort your three vectors, the total number of required floating point operations can be dramatically reduced in spite of the larger number of lines of code needed.  Try the following for large size vectors and see if it isn't faster.

 nx = length(x); ny = length(y); nz = length(z);
 X = [sort(x);inf]; Y = [sort(y);inf]; Z = [sort(z);inf];
 p1 = 0; p2 = 0; p3 = 0; p4 = 0;
 ix = 1; xd = 0; iz = 1; zg = nz;
 f = true;
 for iy = 1:ny
  Y0 = Y(iy);
  if f
   xe = 0;
   while X(ix) <= Y0
    xd = xd + 1;
    if X(ix) == Y0, xe = xe + 1; end
    ix = ix + 1;
   end
   ze = 0;
   while Z(iz) <= Y0
    zg = zg - 1;
    if Z(iz) == Y0, ze = ze + 1; end
    iz = iz + 1;
   end
  end
  p1 = p1 + (xd-xe)*zg; p2 = p2 + xe*zg;
  p3 = p3 + (xd-xe)*ze; p4 = p4 + xe*ze;
  f = Y0 < Y(iy+1);
 end
 n = nx*ny*nz;
 p1 = p1/n; p2 = p2/n; p3 = p3/n; p4 = p4/n; 

  Note: My p1, p2, p3, and p4 are your pith1, pith2, pith3, and pith4, resp.

Roger Stafford
0
7/31/2011 8:38:13 AM
Dear leo nidas,

I think your and Roger's solution are fine already. But your loop method can be improved also: There is no need to expand xx and yy!

 n = length(x) * length(y);
 k1 = zeros(1, n);  % Preallocate !!!
 k2 = zeros(1, n);
 k3 = zeros(1, n);
 k4 = zeros(1, n);
 op=1;
 for i=1:length(x)
     for j=1:length(y)
         xx=x(i);
         yy=y(j);
         k1(op)=sum(xx<yy & yy<z);
         k2(op)=sum(xx<yy & yy==z);
         k3(op)=sum(xx==yy & yy<z);
         k4(op)=sum(xx==yy & yy==z);
         op=op+1;
     end
 end

Next improvement: if xx<yy if TRUE, the number of (xx==yy & yy==z) is zero:
 ...
 for i=1:length(x)
     for j=1:length(y)
         xx=x(i);
         yy=y(j);
         if xx < yy
           k1(op)=sum(yy<z);
           k2(op)=sum(yy==z);
        elseif xx==yy
           k3(op)=sum(yy<z);
           k4(op)=sum(yy==z);
         end
         op=op+1;
     end
 end

Kind regards, Jan
0
7/31/2011 7:36:10 PM
Here is another algorithm, I'll restrict of calculating P(X<=Y<=Z) = p1+p2+p3+p4

%
x=rand(4000,1);
y=rand(5000,1);
z=rand(6000,1);

% Engine
n = [length(x) length(y) length(z)];
[a is] = sort([x(:); y(:); z(:)]);
bins =  1+[0 cumsum(n)];
[trash, loc] = histc(is, bins);
p = [1 0 0 0];

for j = loc.'
    p(j+1) = p(j+1)+p(j);
end
p = p(4)/prod(n)

%%
I'll let the calculation of p1, p2, p3 ans p4 separately as exercise for whom who is still interested in the problem :-)

% Bruno
0
b.luong5955 (6401)
7/31/2011 7:50:31 PM
Reply:

Similar Artilces:

while loop inside a for loop
Hi, can someone please tell me why "time" (t) is not updated correctly within the while loop? Please run this small code in your machine and you'll know what I'm taking about. What I want is: for t=1 run the "while loop" until iq<=(numberc-1) then t=2 run the "while loop" again until iq<=(numberc-1), and so on. Basically, this code runs through all the nonzero elements of A and attempts to divide them individually into two, once a certain time has passed. Please feel free to ask for clarifications. Many thanks in advance. A=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ... 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ... 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ... 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ... 0 0 1 1 1 1 3 3 3 3 5 5 5 5 7 7 7 7 0 0; ... 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ... 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ... 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ... 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 0 0; ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; gridx=20;gridy=10; tarea=16; Td=2;idx=0;iq=1;tsigma=1; maxc=floor((gridx*gridy)/tarea); numberc=max(max(A)); clocks=zeros(25,25); clocks(1:25,1)=1; t=1; for t=1:25 % MCS while iq<=(numberc-1) [rows,cols]=find(A==iq); centroid = mean([rows cols]); mxr=max(rows); mnr=min(rows); mxc=max(cols); mnc=min(cols); if (length(cent...

LOOP and +LOOP
With LOOP, since the index (I) increases by 1 with each loop iteration, we can test for equality as a means to determine if it's time to exit the loop or not: : TEST 10 0 DO I . LOOP ; TEST 0 1 2 3 4 5 6 7 8 9 Here, when I gets to 10, the loop exits. It's not so easy with +LOOP, since the index can be changed by any arbitrary value on each iteration: : TEST 100 0 DO I . RANDOM +LOOP ; Here, we cannot test for equality. We have to test for crossing a boundary instead, at least as far as I can see. With that in mind, are these following results correct: : TEST 10 0 DO I . 1 +LOOP ; TEST 0 1 2 3 4 5 6 7 8 9 10 (11 iterations) : TEST 10 0 DO I . 3 +LOOP ; TEST 0 3 6 9 (4 iterations) : TEST -30 -7 DO I . -3 +LOOP ; TEST -7 -10 -13 -16 -19 -22 -25 -28 (8 iterations) Regards Mark On Nov 25, 10:34=A0pm, MarkWills <markrobertwi...@yahoo.co.uk> wrote: > With LOOP, since the index (I) increases by 1 with each loop > iteration, we can test for equality as a means to determine if it's > time to exit the loop or not: > > : TEST 10 0 DO I . LOOP ; > TEST > 0 1 2 3 4 5 6 7 8 9 > > Here, when I gets to 10, the loop exits. > > It's not so easy with +LOOP, since the index can be changed by any > arbitrary value on each iteration: > > : TEST 100 0 DO I . RANDOM +LOOP ; > > Here, we cannot test for equality. We have to test for crossing a > boundary instead, at least as far as I can see. > > With that in mi...

while loop in a while loop
Hi all, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I am trying to write a program with a while loop within another while loop. When I run the vi both while loops&nbsp; run but then only the inner while loop seems to update, I adjust controls and they have no effect on the indicators. &nbsp; Thanks.. The outer while loop will not update until the inner while loop finishes, at which point the whole dance will start again You need to familiarize with the concept of dataflow programming. Run your VI in "execution highligthing" mode while watching the diagram and you'll get a better feeling on how things work. :) &nbsp; If both loops need to run in parallel, place them next to each other without data dependency. &nbsp; Can you explain what you are trying to do? :) So is it a bad idea to have one within the other if I was them to run simultaniously? Should I put the two whileloops side by side? I have attached the block diagram Thanks &nbsp; Block Diagram.JPG: http://forums.ni.com/attachments/ni/170/319250/1/Block Diagram.JPG sparrowroad wrote:So is it a bad idea to have one within the other if I was them to run simultaniously? Should I put the two whileloops side by side? I have attached the block diagram It all depends what your program should do There are scenarios where stacked loops are appropriate. Typically, one loop is sufficient if you want to run most of the code at 500ms interval, but some p...

loops in loops
In the attached vi, the one second loop runs at one second and can't be touched.&nbsp; In the ten second loop, the ten second clock is a stand in for a process that generates a finished signal. The problem is once the process is complete, to start it again, leave the one second loop running, and start an additional process that will take less time than 10 seconds. thanks internal loops.vi: http://forums.ni.com/attachments/ni/170/319512/1/internal loops.vi Hi exo, what do you try there? If you start your vi, the value from your stop button will be read and after that, both loops "one" second and "ten" second won�t notice the state change of the stop button. Can you please explain, what you try to do?? Mike Okay, let me try again. I need three loops. Two loops start at the same time one runs fast one runs slow. The slow one is probably independent to the problem. When the second loop is done, it needs to restart and start a third loop that takes less than the second. You have a very basic dataflow problems. Unfortunately, I don't understand your description. &nbsp; I would recommend starting with some basic LabVIEW tutorials. &nbsp; Here are some&nbsp;obvious mistakes: - Your loop cannot read changes in the stop button, because the terminal is outside the loop. - You reset "internal loop" to zero in parallel to the loops. You cannot guarantee that this will happen before the locals are read so you might have a race c...

Ending a while loop inside a while loop
How do I make a while loop run as long as the while loop that contains it is running? I tried wiring both conditional terminals to a stop button in the outer loop, but that doesnt seem to work? Is there another way? &nbsp; Thanks. It doesn't work because of data dependency. Once you are inside the inner loop the outer loop will not go to the next iteration until the inner loop is done. It sounds to me like you want parallel loops running, rather than one inside the other. If you open the Example Finder (Help -&gt; Find Examples), and search for "loops", open the examples called "Stopping Parallel While Loops". One has a reset, and one doesn't. Message Edited by smercurio_fc on 04-22-2008 04:55 PM Hi Jasonalan, I am not sure what you are trying to implement, but it is true that the loops are not terminating because of data dependency. The stop button in the outer loop will not be read until the inner loop terminates.If you wish to terminate the two nested loops using a single stop button, you should read the stop button inside the inner loop and wire it to the conditional terminals in both the inner and outer loop. Again, there is no point in doing this unless you have some other logic along with the stop button to stop the loops. If only the stop button is being used to stop both the inner and outer loops, the outer loop will only execute once. In that case, you can just use a sequence structure followed by a single while loop. ...

Nested FOR loops. (a loop within a loop)
I can't get my head around them. I have two MySQL tables, one contains the categories, the other contains the posts. Each post is linked to a category. Now I'm trying to display each post with a drop down menu that has the categories in it and have the category that the post is in selected. Here's my code for the drop down menu where my woes lie. for ($i=0; $i<count($posts); $i++) { for ($j=1; $j<=count($categories); $j++) { if ($j == $post[$i]['id']) { selected = ' selected="selected"'; } else { selected = ''; } $category_list .= '<option value="' . $j . '"' . $selected . '>' . $cat_name[$j] . '</option>'; } unset($j); $dropdown .= '<td><select name="' . $cat_name[$i]['name'] . '">'. $category_list . '</select></td>'; } Here's my problem, for ease, lets say I have 2 categories and 100 posts. For post one I have 2 categories in the menu. For the next post I have 4 categories in the menu (the 2 have been doubled), for the third I have 6 (tripled), and so on. Each post after the next has another 2 categories added. So the drop down menu for post 100 has 200 categories listed. Just the same two repeated over and over. The output should look like this, though an <option> for each category, but just two for this example....

what diference between timed loop and while loop and for loop
If I want to count 2 second working in the loop and next then going out of the loop I don't know&nbsp;which the loop &nbsp;I can use Message Edited by Jairak on 07-26-2008 03:14 PM Why do you need a loop to count for 2 seconds? To learn more about LabVIEW it is recommended that you go through the tutorial(s) and look over the material in the NI Developer Zone's <a href="http://zone.ni.com/devzone/cda/tut/p/id/5054" target="_blank">Learning Center</a> which provides links to other materials and other tutorials. You can also take the <a href="http://www.ni.com/academic/lv_training/how_learn_lv.htm" target="_blank">online courses</a> for free. Hi, &nbsp; From the information you have provided, I don't know exactly what you are trying to do. However, if you want a loop to iterate once, wait until two seconds and then exit the loop, use the "Wait Until Next ms Multiple" function, wire in a numeric constant "2000" and wire a boolean true constant to the loop condition of the while loop to iterate once, or use a for loop with "1" wired into the loop count. &nbsp; Perhaps you need to elaborate your question. Plus, check out the context help (press Ctrl + H) for a consice report on each of the loops. &nbsp; Regards, Jairak wrote: If I want to count 2 second working in the loop and next then going out of the loop I don't know&nbsp;which the loop ...

Re: i am having a problem with while loops, i have two while loops, i have a random number generator inside the &quot;inside&quot; loop and i want to read an array outside the &quot;outer&quot; #2
Hi guys, Apologies about he messy thread - I needed asolution urgently and I didn't get a chance to notice the post date. &nbsp; :Ravens - Thank you a lot. The solution, also found in "my assignmnet live update.vi", works perfectly, doing exactly what I needed. Thanks again. kolorek ...

Improving loop speed with Timed Loop
Hey Everybody, &nbsp; I've got a little bit of an issue that I'm trying to work out: &nbsp; I have a timed loop comprised of 2 sequences.&nbsp; The first sequence measures the current position, compares that position to a position-velocity graph imported from excel, and then compares that commanded velocity to the current velocity to come up with a duty cycle to drive the motor at.&nbsp; The second sequence adjusts the duty cycle appropriately. &nbsp; Ideally, I'd like this to run as fast as possible within the 1Khz clock available (e.g 1ms dt -&gt; 1khz loop speed would be great!). &nbsp; However, when I run this, I find that the dt is actually around 65ms.&nbsp; (I adjusted up to 70ms, because I use the dt to infer velocity as the change in position/time, and wanted to give myself a margin of error). &nbsp; Is it possible to drastically speed this loop up?&nbsp; Are there any array allocations that I could do to make this run faster? &nbsp; I really appreciate any input you guys might have! Thanks! .jim PV_file.csv: http://forums.ni.com/attachments/ni/170/296815/1/PV_file.csv 8-DirControl_01222007.vi: http://forums.ni.com/attachments/ni/170/296815/2/8-DirControl_01222007.vi We can start with a few basic things and see how much they improve performance. Here are my first thoughts:- Why are there two frames in the Timed Loop? This really isn't necessary for anything. The main reason for using frames in a Timed L...

nested loops only loop once
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another way I could write this to get both loops to complete fully? I am using the two while loops to pull data from different tables, and insert that data into a list that has html code surrounding each loop. while ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) { $pn = $url['post_name']; while ($date = mysql_fetch_array($lmdate, MYSQL_ASSOC)) { $lm = $date['post_modified']; echo "<b>"; echo $pn; echo "</b>"; echo "more html"; echo date('Y-m-d', strtotime($lm)); echo "and more html"; } } Thank you. HG toddlahman@gmail.com wrote: > I am using two while loops that are nested. The first loop (post name) > returns the full column of results, but the second (post modified) > only returns the first row of the column. Is there another way I could > write this to get both loops to complete fully? > > I am using the two while loops to pull data from different tables, and > insert that data into a list that has html code surrounding each loop. > > while ($url = mysql_fetch_array($urls, MYSQL_ASSOC)) { > $pn = $url['post_name']; > > while ($date = mysql_fetch_array($lmdate, MYSQL_ASSOC)) { > $lm = $date['post_modified'...

Error -50103 occured with timed loop inside a while loop
Hello everyone,i wrote an application to sample analog voltage from&nbsp; DAQ6024E card (see the attachments).I have a big while loop in the VI because a I want to add some other functions later.In the "WHILE_Cont Acq&amp;Graph Voltage-Int Clk.vi" I use a while loop inside the big while loop to read the samples. It's working properly. but when I use a timed loop inside the big while loop (see "TIMED_LOOP_Cont Acq&amp;Graph Voltage-Int Clk.vi"), I get an error -50103 from the timed loop.&nbsp; It seems that the first cycle is ok, but after the first cycle the error occurs. I don't know what happens with the timed loop, anyone can help me? what does the error -50103 mean? thanks a lot!PS: I am using LabVIEW 8.0Message Edited by molo511 on 10-22-2006 05:21 AMMessage Edited by molo511 on 10-22-2006 05:23 AM TIMED_LOOP_Cont Acq&Graph Voltage-Int Clk.vi: http://forums.ni.com/attachments/ni/170/211534/1/TIMED_LOOP_Cont Acq&Graph Voltage-Int Clk.vi WHILE_Cont Acq&Graph Voltage-Int Clk.vi: http://forums.ni.com/attachments/ni/170/211534/2/WHILE_Cont Acq&Graph Voltage-Int Clk.vi hi molo511, I tested your program but only with simulated devices. I had to delete the wire to the timing, so that the timed while loop runs with 1kHz. Did you already try this? Because this works on my PC. I also found a link in our database that might be interesting for you. &nbsp; <a href="http://digital.ni.com/public.nsf/websear...

Nested parfor loop: Classification of Matrix inside loop
Hello, I am having problems with the following loop, since it is taking too much time. Hence, I would like to use parallel processing, specifically parfor function. Can anyone, please, help me to convert the following 'far' loop into 'parfor'? P = numel(scaleX); % quite BIG number sz = P; start = 1; sqrL = 10; % sqr len e = 200; A = false(sz, sz); parfor m = sz-sqrL/2:(-1)*sqrL:start for n = M(m):-sqrL:1 temp = [scaleX(m), scaleY(m); scaleX(n), scaleY(n)]; d = pdist(temp, 'euclidean'); if d < e A(m, n) = 1; end end end Thank you! "Bek Abdik" <beknazar@unist.ac.kr> writes: > I am having problems with the following loop, since it is taking too > much time. Hence, I would like to use parallel processing, > specifically parfor function. > > Can anyone, please, help me to convert the following 'far' loop into 'parfor'? > > P = numel(scaleX); % quite BIG number > sz = P; > start = 1; > sqrL = 10; % sqr len > e = 200; > A = false(sz, sz); > > parfor m = sz-sqrL/2:(-1)*sqrL:start > for n = M(m):-sqrL:1 > temp = [scaleX(m), scaleY(m); scaleX(n), scaleY(n)]; > d = pdist(temp, 'euclidean'); > if d < e > A(m, n) = 1; > end > end > end As the code analyzer message informs you, the range of a PARFOR l...

Question about keypresses inside loops.
Hello, I need help with something. I trying to write a function that runs something like this: 1) A variable 'x' is initialized and increased by 1 over an in an infinite loop, and the result displayed. 2) When the 'a' key is pressed, x is decreased by one in the loop and result displayed. 3) When the 's' key is pressed, x increases as before. 4) The key 'g' exits the loop. Specifically I'm having trouble with the program detecting a key when pressed while a loop is running. If you use GUI, capture 'KeyPressFcn' callback. If not there is a file on FEX that capture the keyboard. I can't remember the name right now. Bruno ...

How do I initialize one channel of a graph outside a loop, and the other channel INSIDE the loop
I am creating a test-control VI for a motor-control test bench.&nbsp; We have predefined load-cycles that we run depending on the article under test&nbsp;and attributes we wish to "stress".&nbsp; I&nbsp;load these profiles from a&nbsp;spreadsheet (txt file). &nbsp; I want to plot the load profile on a graph...and then, once testing has started, plot a second channel that consists of a single data point (filled to zero) that updates with every second of run-time.&nbsp; This second channel results ina vertical line drawn over the load profile indicating the current position.&nbsp; I run the test inside of a timed loop, and it works great once running. &nbsp; My problem is:&nbsp; Can I initialize the graph OUTSIDE of the timed-loop?&nbsp; I want to draw the test profile on the graph as soon as the operator loads it...which is long before I enter the timed-loop. &nbsp; ThanksMessage Edited by JeffGrimes on 09-03-2006 06:24 PM ...

WAS Re: Question
On Fri, 17 Apr 2009 10:38:03 -0400, Akshaya <akshaya.nathilvar@GMAIL.COM> wrote in PART: >Data want(drop=country pat); > > do until(last.dataset); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > merge dt1(in=a) dt2(in=b); > by dataset; > if a and b; > total=sum(total,pat); > ... .... > end; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > >Run; > I am just curious and wanting to know why the merge is inside a do loop? Thanks, -TJ ...

Re: WAS Re: Question
This is based on DOW loop, and the original poster requires one record/dataset(variable). Please refer to this<http://support.sas.com/resources/papers/proceedings09/038-2009.pdf>wonderful paper by Paul Dorfman on DOW loop. On Fri, Apr 17, 2009 at 11:37 AM, T J <tj_noreply@yahoo.com> wrote: > On Fri, 17 Apr 2009 10:38:03 -0400, Akshaya <akshaya.nathilvar@GMAIL.COM> > wrote in PART: > > >Data want(drop=country pat); > > > > do until(last.dataset); > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > > merge dt1(in=a) dt2(in=b); > > by dataset; > > if a and b; > > total=sum(total,pat); > > ... .... > > end; > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > > >Run; > > > > I am just curious and wanting to know why the merge is inside a do loop? > > Thanks, > > -TJ > -- AkshayA! ...

how to speed up a loop
Hiya, I would be grateful if anyone could suggest how I might speed up my code a bit, this loop is taking 10 hrs to execute and I KNOW my very basic matlab knowledge is the main cause...Thanks in advance. for ii=1:length(lats); for jj=1:length(longs); if ~isnan(mean(EKE(ii,jj,:))) trend=nan(1,1000); for i=1:1000 %Number of bootstrap samples randblock = unidrnd(num_blocks,1,num_blocks); %Choose which blocks to use Ind_sim = indices(:,randblock); %Find which data are in each block Ind_sim = Ind_sim(:); %Stack these block into one big year Ysim = ets(Ind_sim); %Construct the Y data trnd=polyfit(date,Ysim,1); trend(i)=trnd(1); end end realtrend=polyfit(date,squeeze(EKE(ii,jj,:)),1); realtrend=realtrend(1); upperlim=mean(trend)+2*std(trend); lowerlim=mean(trend)-2*std(trend); if realtrend>=upperlim || realtrend<=lowerlim mask(ii,jj)=1; else mask(ii,jj)=0; end end end "Chris ODonnell" wrote in message <j88uo5$d0e$1@newscl01ah.mathworks.com>... > Hiya, > I would be grateful if anyone could suggest how I might speed up my code a bit, this loop is taking 10 hrs to execute and I KNOW my very basic matlab knowledge is the main cause...Thanks i...

while loop question
I am processing a form submission using POST method. Since some of the fields are going to be blank and I want to eliminate getting those in the email sent to me, I have put this code together that seems to work okay and not throw any errors: <?php error_reporting (E_ALL); while(list($key, $val) = each($_POST)){ if(is_array($val)){ foreach($val as $element) $mesg .= "$key: $element\n"; } if($val == "") { unset($key,$val); } else $mesg .= "$key: $val\n\n"; } $to = 'foo@foo.com'; $subject = 'Info Submission'; $header = "From: $_POST[email]\r\n"; mail( $to, $subject, $mesg, $header ); ?> But what puzzles me is that when I echo the results of $mesg to the page, it's fine. But when it sends to me in an email, I get an extra period (.) about five or six lines down on every submission and I can't find out why. I know it's not exactly an earth-shaking problem but I'm curious as to what I may have done wrong so I can correct it and make sure I don't repeat it in the future. Thanks for any help. ...

Speed up of for loop
hey, I am trying to speed up a part of my code: % Variables x=0:0.001:1; tau=0.1:0.01:0.3; b=rand(length(tau),1); n=length(x); m=length(tau); A = ones(1,m); A(1)=0.5; A(m)=0.5; A = A*x(2); % preallocate iComp iComp=zeros(n,1); % computation for l = 1:n integrand = b.*exp(-x(l)./tau'); iComp(l) = A * integrand; end Is there some way to avoid the for loop? I tryied arrayfun but it was twice as slow as the for loop... Thanks in advance, Norman "Norman F." wrote in message <jf8p66$11t$1@newscl01ah.mathworks.com>... > % Variables > x=0:0.001:1; > tau=0.1:0.01:0.3; > b=rand(length(tau),1); > n=length(x); > m=length(tau); > > A = ones(1,m); > A(1)=0.5; A(m)=0.5; > A = A*x(2); > > % preallocate iComp > iComp=zeros(n,1); > > % computation > for l = 1:n > integrand = b.*exp(-x(l)./tau'); > iComp(l) = A * integrand; > end > > Is there some way to avoid the for loop? I tryied arrayfun but it was twice as slow as the for loop... - - - - - - - - - There's no need to multiply by A each of those thousand times. Instead, make the appropriate adjustment in b ahead of time. % Variables (as before) x=0:0.001:1; tau=0.1:0.01:0.3; b=rand(length(tau),1); n=length(x); m=length(tau); % An alternative b2 = b/1000; b2([1,m]) = b2([1,m])/2; iComp = exp((-x).'*(1./tau))*b2; I doubt that this will prove to be very much faster though, because the main comput...

Do Loop Question
I have a form that asks for the month, the year, the number of weeks in the month and the beginning date for the month. I want to be able to: Number the weeks (e.g. 1, 2,3, etc.) Assign a beginning date and an ending date for each week Loop this for the number of weeks as input on the form Transfer each week's number with corresponding beginning and ending dates to a table Thanks for the help in advance. Troy Lee troy_lee@comcast.net wrote: > I have a form that asks for the month, the year, the number of weeks > in the month and the beginning date for the month. > > I want to be able to: > > Number the weeks (e.g. 1, 2,3, etc.) > Assign a beginning date and an ending date for each week > Loop this for the number of weeks as input on the form > Transfer each week's number with corresponding beginning and ending > dates to a table > > Thanks for the help in advance. > > Troy Lee There's some missing elements in your problem/question. Look at the Format() function in help. You can set/determine the first day of a week with one argument. vbUseSystem 0 Use NLS API setting. VbSunday 1 Sunday (default) vbMonday 2 Monday vbTuesday 3 Tuesday vbWednesday 4 Wednesday vbThursday 5 Thursday vbFriday 6 Friday vbSaturday 7 Saturday You can also set what indicates the first week of the year. vbUseSystem 0 Use NLS API setting. vbFirstJan1 1 Start with week in which January 1 occurs (default). vbFirstFourDays 2 St...

Matlab parallel for loop or Matlab open pool
I am trying to to some computations and I would like to do it in parallel using parfor or by Opening the matlabpool.. as the current implementations is too slow: result=zeros(25,16000); for i = 1:length(vector1) % length is 25 for j = 1:length(vector2) % length is 16000 temp1 = vector1(i); temp2 = vector2(j); t1 = load(matfiles1(temp1).name) %load image1 from matfile1 t2 = load(matfiles2(temp2).name) % load image2 from matfile2 result(i,j)=t1.*t2 end end It work fine but I would really like to know if there is a way to speed thing up ... Thanks a lot in advance! ...

loops in parfor loops incredibly slow
Hi, I am currently debugging why my parallel application does not yield the expected speedup. I trimmed it down to the following minimal example, which results in a speed loss of about a factor of 12 when the outer loop is run as a parfor loop (runtimes 0.35s [for] compared to 4.4s [parfor] on my system, but you can easily scale this by means of the loop index limits): tic; for UnusedLoopIndex = 1 : 1e4 % switch this one between for and parfor for AnotherUnusedLoopIndex = 1 : 1e4, end end toc; I also have a slightly more complex example with a speedup loss of about 150, and a more practical one where I only observe a loss of 3 (depending on the loop body). I tried two setups: 1) parfor using the current MATLAB client (no matlabpool open, matlabpool('size') == 0) 2) parfor using one worker (matlabpool open 1, matlabpool('size') == 1) These give the same results (runtimes), both slower than for loops. Does anyone have a clue what is going on there? Do loops in parfor loops really pose problems? Note that replacing the inner loop by something like rand(30,30)*rand(30,30); does result in correct behavior, that is, equal runtimes for both for and parfor loops. Thanks Yannick I just got a helpful reply from the Mathworks support: The reason for this behavior is that the workers are not able to use the MATLAB accelerator on the plain parfor body, so the body is run as if "feature accel off" is set in the "for" loo...

do while looping
mine is simple application of do while loop in matlab 2010a i=0; do { i=i+1; display(i); } while i<10 this has 3 errors and after a lot of efforts ,m unable to get it correct... On 4/9/2012 4:37 PM, rahul wrote: > mine is simple application of do while loop in matlab 2010a > i=0; > do > { > i=i+1; > display(i); > } > while i<10 > > this has 3 errors and after a lot of efforts ,m unable to get it correct... i=0; while i<10 i=i+1; disp(i); end Or just leave off the semicolon % Don't put unnecessary blank lines in command window. format compact; % Display workspace panel with variables. workspace; k=0; while k<10 k= k+1 end Or use the fprintf() function: k=0; while k < 10 k = k+1; fprintf('k = %d\n', k); end Note how I used k as a loop index to avoid overwriting the imaginary constant "i" (square root of -1). Similarly, it's recommended not to use j either because it's also the imaginary constant. ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c4213ad3-64f4-4dd5-9044-071df0f6fca1@h5g2000vbx.googlegroups.com>... > Or just leave off the semicolon > > % Don't put unnecessary blank lines in command window. > format compact; > % Display workspace panel with variables. > workspace; > > k=0; > while k<10 > k= k+1 > ...

getting a value from a while loop to input back into the loop
This program is supposed to recognize an input voltage of 5V and then proceed to start&nbsp;the vertical bar picture box moving either left or right, per the users choice. The user can also input the desired bar width, scroll speed, and cycle duration (time to get from left to right and back). I want the picture box to move left and right repeately and smoothly. On frame #3, I created what I believe to be a state machine. The outer while loop should obtain the resulting position value from the inner while loop and then use that value as the new starting position. For some reason, the picture box will start moving left (if "Left" is chosen as the initial direction) and then remain on the left while shaking. It should rather move from the left back to the right and back left again. How do I fix this problem? Thank you!! &nbsp; (the first attachment is with the state machine. the second has the same problem as the first but has a less complicated format) &nbsp; vert rect with state machine.vi: http://forums.ni.com/attachments/ni/170/193862/1/vert rect with state machine.vi vert rect without state machine.vi: http://forums.ni.com/attachments/ni/170/193862/2/vert rect without state machine.vi I am unable to view your changes because I only have LabView 7.0. Could you post print screen images of your changes? Thank you so much! ...

Web resources about - speed up for loop inside for loop short question - comp.soft-sys.matlab

West Lothian question - Wikipedia, the free encyclopedia
He illustrated his point by pointing out the absurdity of a Member of Parliament for West Lothian being able to vote on matters affecting the ...

To daylight save or not to daylight save? That is the question
Australians are perfectly happy to shrug over boring, vital issues &ndash; but force our free citizenry to occasionally adjust their clocks? ...

To SQL or NoSQL? That’s the database question
It's a tangled, database web out there. (credit: Getty Images) Poke around the infrastructure of any startup website or mobile app these days, ...

Question of the Day: Coolest engine-displacement badge ever?
Filed under: Etc. , Automotive History During the engine-displacement arms race, car companies vied for the raciest-looking numerical badges. ...

Yahoo!: SunTrust to Host Call with Proxy Advisory Group on Starboard Questions
SunTrust Robinson Humphrey ’s Robert Peck , who has beaten the drum louder than anyone else on the Street about the lack of responsiveness of ...

Apple responds to FBI iPhone hack, but questions remain
Yesterday, the FBI announced that it had managed to break into the San Bernardino shooter's iPhone sans help from Apple. The iPhone manufacturer ...

4 Questions That Unlock Brand Potential
It’s difficult to escape the pervasive influence that brands have on our lives. In the broadest sense, brands constitute the organizing mechanism ...

A Student’s Question About the Minimum Wage
(Don Boudreaux) Tweet Last night in my Principles of Microeconomics class at George Mason University (ECON 103) my students and I discussed ...

​The 10 oddest job interview questions
On your toes, applicants: What's your game plan if you find a penguin in the freezer?

Nearly retired? 3 questions to ask
No matter what happens to the markets, retirees should ask these three key questions to keep money safe, say financial advisors.

Resources last updated: 3/30/2016 5:45:32 PM