How to change values between two values in an array

I need to replace certain values within an array that fall between 2 and 4. The code should find where the array equals 2 and then find the following location where the array equals 4 and replace all values with 3. Then it should find where the array equals 4 and then find the following location where the array equals 2 and replaces all of those values with 1. And then loop through the array until all values are 1, 2, 3 or 4 and in order (repeats of values are OK).
Here is an example array:
transitions = [1285738, 2, 2, 2915260, 4, 4290129, 2, 5650291, 4, 8030363, 2, 9337983, 4, 12040647];
I need to:
  1. change the values that fall between 2 and 4 to 3 (e.g., the 4th, 8th and 12th values should be 3)
  2. change the values between 4 and 2 to 1 (e.g., the 6th and 10th values should be 1)

5 Comments

Are you saying that the first element will eventually be equal to 1,2,3 or 4? Because I do not see how your transition rules get you there.
The final result should be:
transitions = [1, 2, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1];
As for the first and last points, I can add a rule to change those. What I'm stuck on though is how to find the points in between 2 and 4 and then in between 4 and 2.
The requirements statement is still ambiguous.
"array equals 2 and ... array equals 4" should become 3
then
"array equals 4 and ... array equals 2" should become 1
If the first operation is performed, then the second condition is never true. If we're to assume that what you mean is that everything in some interval bounded by 2 and 4 is to be converted to 3, and all else is to be converted to 1, then it's still unclear whether 2 or 4 convert to 1 or 3. Your statements say both.
To add to the confusion,
"And then loop through the array until all values are 1, 2, 3 or 4 and in order"
Why would the result still contain 2 or 4? Why would it be in order? What order?
Let's say I assume the following:
values in the closed interval [2 4] become 3
else, they become 1
transitions = [1285738, 2, 2, 2915260, 4, 4290129, 2, 5650291, 4, 8030363, 2, 9337983, 4, 12040647];
ininterval = (transitions>=2) & (transitions<=4);
transitions(ininterval) = 3;
transitions(~ininterval) = 1;
transitions
transitions = 1×14
1 3 3 1 3 1 3 1 3 1 3 1 3 1
If that's not what you're after, you'll have to clarify.
I understood @Jake Jordan's rules to be:
  1. If a 4 follows a 2, change every intervening value to 3, THEN
  2. If a 2 follows a 4, change every intervening value to 1
DGM
DGM on 4 Oct 2021
Edited: DGM on 4 Oct 2021
... oh.
I guess "between" is one of those expressions like "in a row" that are sneakily ambiguous when talking about array structures.
EDIT: oof. I didn't even see OP's comment.

Sign in to comment.

Answers (1)

Hi Jake,
I am assuming you want to replace values in the array that occur between a 2 and 4, and those that occur between a 4 and 2.
One way of doing this I can think of is using the "find" function to get the indices of all the 2s and 4s as demonstrated below
transitions = [1285738, 2, 2, 2915260, 4, 4290129, 2, 5650291, 4, 8030363, 2, 9337983, 4, 12040647];
t=find(transitions==2)
t = 1×4
2 3 7 11
f=find(transitions==4)
f = 1×3
5 9 13
you can then loop over 'f' and 't' and replace the values in between accordingly
you can find more information and how-to examples on the "find" function in the documentation
Hope this helps

Asked:

on 4 Oct 2021

Answered:

on 7 Oct 2021

Community Treasure Hunt

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

Start Hunting!