Ely
Ely

Reputation: 115

Matlab: delete duplicate consecutive values in an array

I have this array

Y=[618 1 631 618 631 618 631 631 631 618 631 627 631 631]

and I want to trasform it in:

[618 1 631 618 631 618 631 618 631 627 631]

I have used this code

[~,index] = unique(Y,'first');       
Y(sort(index))

but the answer is this, that it's different from the result that I want. ans = 618 1 631 627

Can you help me?

Upvotes: 3

Views: 318

Answers (1)

GameOfThrows
GameOfThrows

Reputation: 4510

How about this? (I really thought this was a duplicate, but I did not find one that had similar title - I'm sure the content came up somewhere)

NewY = Y([1,diff(Y)]~=0)

NewY =  
618 1 631 618 631 618 631 618 631 627 631

You can also save a byte in the notation by doing:

NewY = Y(~~[1,diff(Y)])

As suggested by @AndrasDeak :D

Upvotes: 5

Related Questions