help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: blocking nested cycles


From: David Bateman
Subject: Re: blocking nested cycles
Date: Tue, 29 Jun 2010 04:32:46 -0700 (PDT)


octave_user wrote:
> 
> Hi!
> I created a function in Matlab, which has a problem. The code is the
> following:
> 
> ==============
> for i=1:m
>  for j=1:n
>   for jj=1:m   
>    if x(j)<tresholdX(jj) & y(j)<tresholdY(jj) 
>         ET=[ET; i j];
>         break
>     end
>    end
>   end
> end
> ==============
> basically, when the "if" clause in the three nested "for" cycles is
> verified, I should skip right to the next "i" values
> As it is now programmed, the function only blocks the last "jj" cycle;
> this because in Matlab only one "break" command can be used, blocking the
> innermost cycle only
> 
> Is there any way to get around this problem? Is it possible in Octave to
> block more cycles?
> In FORTRAN (or even in Basic) there was the "goto" command, by which you
> could solve this problem
> 
> Any suggestion?
> Thanks!
> 

The way your code is written the outer loop could be complete dropped and
replaced by a call to repmat. Why not just vectorize completely this code.
Doing this your code might be wriiten as something like

idx = rem (repmat(0:m-1, m, 1) + repmat([n:-1:1]', 1, n), m) + 1; 
[i, j] = find (repmat(x(:), 1, m) < thresholdX(idx) & repmat(y(:), 1, m) <
thresholdY(idx));
## Simulate the break
idx = i != i (n, 1:n-1);
ET = [i(idx)(:), j(idx)(:)];
ET = repmat (ET, m, 1);  ## Replace outer loop

This code is completely unchecked and the ordering of ET is probably
different that you had, but it should be very close to what you want. The
code could probably also be written using bsxfun and save some memory. But
bsxfun is rather slow in 3.2.x relative to its speed in the development
version, so I wouldn't recommend that solution till Octave 3.4.x comes out.

D.

-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/blocking-nested-cycles-tp2271946p2272063.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]