0

I am using Matlab to modify some code that will be used in a real time system. The real time system can't use 'break' or 'return' statements. I have a bunch of for loops in Matlab that use 'break' or 'return'.

This is an example:

for j = find(vec == 0)
   if A(j) == 1
      break;
   end
end

How do I get around using the 'break' statements? I was told that I can use a 'while' loop instead. however, I am trying to see if there are other ways.
This seems like it should be a basic question but I can't think of other solutions right now.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
mjpablo23
  • 681
  • 1
  • 7
  • 23
  • When you are stating `j = find(vec == 0)`, you are using `j` as a vector and not as an iterator. Are you sure you want that? – Divakar Aug 13 '14 at 16:54
  • 1
    @Divakar Well, now I got your idea. It's unlikely OP makes a mistake here, or otherwise he/she will run into another problem. – Yvon Aug 13 '14 at 17:19
  • Or just tell us what you plan to achieve with such a code. – Divakar Aug 13 '14 at 18:37

1 Answers1

0

A for with break is equivalent to a while.

for jj = find(vec == 0)
   if A(jj) == 1
      break;
   end
end

....

ind = find(vec == 0);
p = 1;
while ( A(ind(p)) ~= 1 && p<length(ind) )
    p = p+1;
end
p = ind(p);

bonus: using i and j is bad: Using i and j as variables in Matlab

Community
  • 1
  • 1
Yvon
  • 2,903
  • 1
  • 14
  • 36