Got Questions? Get Answers.
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:
Combining adjacent values --> removing redundant zeros

Subject: Combining adjacent values --> removing redundant zeros

From: Pat Finder

Date: 18 Aug, 2008 15:55:04

Message: 1 of 4

I have a vector that contains various value:

given = [ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];

I want to combine all the zeros into a single instance.

desired = [ 0 -1 2 0 1 1 0 -5 0 6 ];

Does anyone know an easy way to do this in Matlab?

Thanks.

Subject: Combining adjacent values --> removing redundant zeros

From: Alan B

Date: 18 Aug, 2008 16:24:02

Message: 2 of 4

The best I can think of:
a=[ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];
a( (a(1:end-1)==0) & (a(2:end)==0) )=[];

"Pat Finder" <pfinder.nospam@netacc.net> wrote in message
<g8c60o$ffi$1@fred.mathworks.com>...
> I have a vector that contains various value:
>
> given = [ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];
>
> I want to combine all the zeros into a single instance.
>
> desired = [ 0 -1 2 0 1 1 0 -5 0 6 ];
>
> Does anyone know an easy way to do this in Matlab?
>
> Thanks.
>

Subject: Combining adjacent values --> removing redundant zeros

From: Pat Finder

Date: 18 Aug, 2008 16:54:02

Message: 3 of 4

Thank you Alan. That was exactly the kind of short-cut I
was looking for. I'm not good enough at Matlab to figure
this kind of thing out yet.

Thank you!

Best,

- Pat

"Alan B" <monguin61@yahoo.com> wrote in message
<g8c7n2$6ea$1@fred.mathworks.com>...
> The best I can think of:
> a=[ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];
> a( (a(1:end-1)==0) & (a(2:end)==0) )=[];
>
> "Pat Finder" <pfinder.nospam@netacc.net> wrote in message
> <g8c60o$ffi$1@fred.mathworks.com>...
> > I have a vector that contains various value:
> >
> > given = [ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];
> >
> > I want to combine all the zeros into a single instance.
> >
> > desired = [ 0 -1 2 0 1 1 0 -5 0 6 ];
> >
> > Does anyone know an easy way to do this in Matlab?
> >
> > Thanks.
> >
>

Subject: Combining adjacent values --> removing redundant zeros

From: Walter Roberson

Date: 18 Aug, 2008 17:17:41

Message: 4 of 4

Pat Finder wrote:
> I have a vector that contains various value:
> given = [ 0 -1 2 0 0 0 0 1 1 0 0 0 -5 0 0 6 ];
> I want to combine all the zeros into a single instance.
> desired = [ 0 -1 2 0 1 1 0 -5 0 6 ];
> Does anyone know an easy way to do this in Matlab?

isz = given == 0;
desired = given(~[false (isz(2:end) & isz(1:end-1))])


--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Tags for 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