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

2 Ansichten (letzte 30 Tage)
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];

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by