3

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?

2
  • 1
    Can you explain what language this is ? Commented Mar 18, 2016 at 16:19
  • Sorry, it's matlab: I have add the tag Commented Mar 18, 2016 at 16:20

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.