0

the easiest way for me to explain what i want is with an example:

a = 1:20

b = [2,7,12,18]

Now I want c to be [1,3,4,5,6,8,...,19,20] with length 16: length(a) - length(b) of course.

Is there a way for me to get c?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Antonio Craveiro
  • 121
  • 1
  • 11

2 Answers2

5

You can delete array elements using x(3)=[]

c=a;
c(b)=[];
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    works for this simple case. But for the general case (where `b` contains elements to be removed from `a` instead of indices), use `setdiff`, as in the other answer. – Rody Oldenhuis Feb 10 '14 at 09:14
4

What you want is called set difference in most languages. In MATLAB, you can use the setdiff function:

a=1:20;
>> b=[2,7,12,18];
>> setdiff(a,b);

ans =

Columns 1 through 11

 1     3     4     5     6     8     9    10    11    13    14

Columns 12 through 16

15    16    17    19    20
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Bartomeu Galmés
  • 263
  • 1
  • 5
  • 14