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

3 次查看(过去 30 天)
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];

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by