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

조회 수: 9 (최근 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