Removing Elements from an Array based on their Position for every 3 Elements

1 view (last 30 days)
Say I have an array called elements_n:
elements_n = [0, 1];
I have another array called Arr:
Arr = [0, 1, 3, 1, 2, 4];
If any of the first 3 elements of the array Arr are equal to the first element of elements_n which is 0, I would like to delete that element from the array called Arr. I then repeat the same process for the next 3 elements of the Array Arr. So to explain myself better, I will use the following example:
Compare the first 3 elements of array Arr which are 0, 1, 3 to the first element of elements_n which is 0. Since Arr[1] == elements_n[1]. I delete Arr[1] from the array Arr.
Compare the next 3 elements of array Arr which are 1, 2, 4 to the second element of elements_n which is 1. Since Arr[4] == elements_n[2]. I delete Arr[4] from the array Arr. So the elements that should be left in the array Arr are:
Arr = [1, 3, 2, 4];

Accepted Answer

Adam
Adam on 21 Jun 2018
If you are using R2015a or later then this will work:
elements_n = repelem( elements_n, 3 );
Arr( Arr == elements_n ) = [];

More Answers (0)

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!