sifis
sifis

Reputation: 21

vector which skip's a step

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

Answers (3)

Daniel
Daniel

Reputation: 36710

I would use setdiff

>> setdiff(-5:5,1)

ans =

    -5    -4    -3    -2    -1     0     2     3     4     5

Upvotes: 10

rayryeng
rayryeng

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

Tomis Aristidou
Tomis Aristidou

Reputation: 1

You can try setting it manually to " ". eg x(10)=[];

Upvotes: 0

Related Questions