Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
shortening a vector

Subject: shortening a vector

From: jem

Date: 3 Dec, 2010 02:49:07

Message: 1 of 3

All,

How do i combine cells with the same content to a single cell?
i.e
I want to shorten [1 2 3 3 3 4 5 5 6 6 6 ] to [1 2 3 4 5 6] without using loops?

Thanks.

Sorry for the troubles.

jem

Subject: shortening a vector

From: Walter Roberson

Date: 3 Dec, 2010 03:01:32

Message: 2 of 3

On 02/12/10 8:49 PM, jem wrote:

> How do i combine cells with the same content to a single cell?
> i.e
> I want to shorten [1 2 3 3 3 4 5 5 6 6 6 ] to [1 2 3 4 5 6] without
> using loops?

unique([1 2 3 3 3 4 5 5 6 6 6 ])

Note: unique will sort the output. Which happens to match in this case,
but your posting leaves open the possibilities that the runs might be
out of order. Your posting also leaves open the question about what to
do if the values are separated -- e.g., if you had [1 2 3 3 5 4 3 4 5 6
6 6] then should the output be [1 2 3 5 4 3 4 5 6] because that is the
consequative runs, or should it be [1 2 3 5 4 6] because the "later" 3
should be merged in with the earlier 3's but the order of the unique
elements should stay the same ?

Subject: shortening a vector

From: Roger Stafford

Date: 3 Dec, 2010 03:33:05

Message: 3 of 3

"jem " <jeramymendoza@gmail.com> wrote in message <id9lr2$boi$1@fred.mathworks.com>...
> All,
>
> How do i combine cells with the same content to a single cell?
> i.e
> I want to shorten [1 2 3 3 3 4 5 5 6 6 6 ] to [1 2 3 4 5 6] without using loops?
>
> Thanks.
>
> Sorry for the troubles.
>
> jem
- - - - - - - -
  I would guess you wish to delete adjacent elements which are alike, as in

 x = [8 7 8 8 4 4 4 8 7 7 4] --> y = [8 7 8 4 8 7 4]

If so, do this:

 y = x([true,diff(x)~=0]);

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us