Reputation: 21
I want to create a vector without the number 1 .
x=-10:1:10;
To avoid this:
for(n=0:21)
if(x(n)==1)
x(n)=[];
end
end
What can I do ?
Upvotes: 1
Views: 91
Reputation: 36710
I would use setdiff
>> setdiff(-5:5,1)
ans =
-5 -4 -3 -2 -1 0 2 3 4 5
Upvotes: 10
Reputation: 104514
Instead of manually generating a vector from -10 to 10 and removing the entry that has the value of 1, you can always use colon
/ :
and not include 1 in the vector instead. Something like:
x = [-10:0 2:10];
Because it's such a small vector, you probably won't gain much by doing it this way in comparison to fully generating the vector and removing one entry as per David's suggestion. I do agree with David though. Learn logical indexing! It's one of the backbones for making any MATLAB code fast.
Upvotes: 7