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

3 ビュー (過去 30 日間)
CharlesB
CharlesB 2018 年 6 月 21 日
コメント済み: CharlesB 2018 年 6 月 22 日
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 Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by