All entries of matlab array except certain index

Alec Jacobson

September 07, 2011

weblog/

Say you have a matrix D:
D = [1 2 3 4 5];
And you want to be able to suck out all entries that are not index i. You can do this with the following:
D(1:end ~= i)
So then the following is produced
i = 1;
D(1:end ~= i)
ans =

     2     3     4     5

i = 2;
D(1:end ~= i)
ans =

     1     3     4     5

i = 5;
D(1:end ~= i)
ans =

     1     2     3     4