blocking nested cycles

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

blocking nested cycles

octave_user
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!
Reply | Threaded
Open this post in threaded view
|

Re: blocking nested cycles

Jaroslav Hajek-2
On Tue, Jun 29, 2010 at 11:38 AM, octave_user <[hidden email]> 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

You can use an extra control variable tested in the outer loops, or
make this a function and use return.
Of course, an even much better idea is to get rid of the loops
completely and use array masks and find instead.

--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: blocking nested cycles

David Bateman
In reply to this post by octave_user
<quote author="octave_user">
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!
</quote>

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.