Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Break statements still result in the first condition breaking array element being shown - how can it be stopped from being generated?

Asked by Thom on 3 Dec 2011

Just a little annoying that matlab throws up the value that breaks a condition in the case below - can the code below be changed to solve this please?

For example

A = [ 2 3 4 5 6 9 9 10] %(elements of A matches t)
for t = 1:8;
B(t) = 3*A(t);
if B(t) >= 24;
break
elseif B(t) <= 3;
break
end
end
end

I would like to get from this B = [6 9 12 15 18]- the last three exceed 24.

Instead I get B = [6 9 12 15 18 27]

How can the code be changed so that B(6) = 27 will not be printed?

Many Thanks!

0 Comments

Thom

Products

No products are associated with this question.

2 Answers

Answer by Fangjun Jiang on 3 Dec 2011
Accepted answer

One solution:

%%
A = [ 2 3 4 5 6 9 9 10] %(elements of A matches t)
for t = 1:8;
B(t) = 3*A(t);
if B(t) >= 24 || B(t)<=3
    B(t)=[];
    break
end
end

0 Comments

Fangjun Jiang
Answer by Naz on 3 Dec 2011

As far as I understand, you don't care weather you reach the end of the A array or not. As soon as you hit the fist value that is out of (3 24) bound you want to break. Is that right? If so, then you can do the following:

A = [ 2 3 4 5 6 9 9 10] %(elements of A matches t)
for t = 1:length(A)
if 3*A(t) >= 24 || 3*A(t) <=3 
break;
else
B(t) = 3*A(t);
end
end

0 Comments

Naz

Contact us