Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all elements except one in a vector

My question is very similar to this one but I can't manage exactly how to apply that answer to my problem.

I am looping through a vector with a variable k and want to select the whole vector except the single value at index k.

Any idea?

for k = 1:length(vector)

   newVector = vector( exluding index k);    <---- what mask should I use? 
   % other operations to do with the newVector

end
like image 530
Matteo Avatar asked Oct 25 '13 17:10

Matteo


2 Answers

Another alternative without setdiff() is

vector(1:end ~= k)
like image 123
ChaoticByNature Avatar answered Oct 19 '22 00:10

ChaoticByNature


vector([1:k-1 k+1:end]) will do. Depending on the other operations, there may be a better way to handle this, though.

For completeness, if you want to remove one element, you do not need to go the vector = vector([1:k-1 k+1:end]) route, you can use vector(k)=[];

like image 10
arne.b Avatar answered Oct 19 '22 01:10

arne.b