- Does B ever change?
- How complicated is the Ai/B comparison (e.g., a simple inequality)?
- Do you start with a full A vector or are you building it as you go?
- Does the procedure depend on other A's. For example, can the A2 update/check run independent of A1? A3?
- "with the updated value of A1 in 3 (second iteration)" what do you mean by '3'?
- Please reread your description and edit for clarity

# create a while loop with varying conditions.

1 view (last 30 days)

Show older comments

How can I make a while script capable of performing the following task:

A = [A1 A2 A3 .............. An]

B an abitrary value

1. Compare with (whlile) A1 com B

if true, update the value of A1 (code) and re-compare (While) with the updated value of A1 in 2 (second iteration)..

If false, store A1, stay in the routine for the next one, which would be A2, and remove A1 from the rest of the comparisons to be made (end operation with A1).

2. Compare (While) with the new value of A1 with B (In case the first itinerary was true) and A2 with B.

if the comparison of A1 with B is true, update the value of A1 and re-compare (While) with the updated value of A1 in 3 (second iteration)..

IF the comparison of A1 with B is false, store A1 stay in the routine for the next one, which would be A3, and remove A1 from the rest of the comparisons to be made (end operation with A1).

if the comparison of A2 with B is true, update the value of A2 and re-compare (While) with the updated value of A2 in 3 (second iteration).

IF the comparison of A2 with B is false, store A2 stay in the routine for the next one, which would be A3, and remove A2from the rest of the comparisons to be made (end operation with A2).

The process continues until it reaches the last element of A

##### 3 Comments

Sindar
on 17 Oct 2020

That doesn't really answer questions 3-6, but I'll give it a go:

% create an A vector, and a B for comparison

A=1:10;

B = 10;

% find locations where Ai<=B

idx = A<=B;

% update these locations to be 2

A(idx) = 2;

% find locations where new Ai<B

idx = A<=B;

% update these locations to be 3

A(idx) = 3;

or, if you want to continue through to 10:

% create an A vector, and a B for comparison

A=1:10;

B = 10;

for ind=2:10

% find locations where Ai<=B

idx = A<=B;

% update these locations to be ind (2, 3, ..., 10)

A(idx) = ind;

end

if you only want to update the first Ai less than B:

% create an A vector, and a B for comparison

A=1:10;

B = 10;

% find first where Ai<=B

idx = find(A<B,1,'first');

% update this location to be 2

A(idx) = 2;

Does that help?

### Answers (0)

### See Also

### Categories

### Products

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!