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:
How to merge two vectors using special rule?

Subject: How to merge two vectors using special rule?

From: Rob Joy

Date: 31 Dec, 2008 20:18:02

Message: 1 of 8

Hallo,

I have following Problem:

I would like to merge this two vectors. Each new starting block of ones or minus ones in a or b overrides the other already running block of ones or minus ones.

a = [0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 ]';
b = [0 0 0 0 -1 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 0]';

The result schould be like this:

result = [0 0 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 1 1 1 1]';

Another question is, how to get idx of starting blocks of ones and minus ones in result.
Should generate [3 5 10 12 15]

Have somebody an Idea

Rob

Subject: How to merge two vectors using special rule?

From: Matt

Date: 31 Dec, 2008 21:30:20

Message: 2 of 8

"Rob Joy" <joek91@yahoo.com> wrote in message <gjgk1q$juh$1@fred.mathworks.com>...
> Hallo,
>
> I have following Problem:
>
> I would like to merge this two vectors. Each new starting block of ones or minus ones in a or b overrides the other already running block of ones or minus ones.
>
> a = [0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 ]';
> b = [0 0 0 0 -1 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 0]';
>
> The result schould be like this:
>
> result = [0 0 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 1 1 1 1]';
>
> Another question is, how to get idx of starting blocks of ones and minus ones in result.
> Should generate [3 5 10 12 15]

What happens if a block of -1s in "a" starts running at the same point as a block of 1s in "b"? Which overrides the other?

Subject: How to merge two vectors using special rule?

From: Roger Stafford

Date: 31 Dec, 2008 22:20:02

Message: 3 of 8

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjgo9c$3g9$1@fred.mathworks.com>...
> "Rob Joy" <joek91@yahoo.com> wrote in message <gjgk1q$juh$1@fred.mathworks.com>...
> > Hallo,
> >
> > I have following Problem:
> >
> > I would like to merge this two vectors. Each new starting block of ones or minus ones in a or b overrides the other already running block of ones or minus ones.
> >
> > a = [0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 ]';
> > b = [0 0 0 0 -1 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 0]';
> >
> > The result schould be like this:
> >
> > result = [0 0 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 1 1 1 1]';
> >
> > Another question is, how to get idx of starting blocks of ones and minus ones in result.
> > Should generate [3 5 10 12 15]
>
> What happens if a block of -1s in "a" starts running at the same point as a block of 1s in "b"? Which overrides the other?

  Besides Matt's question there is also the matter of how to proceed if one block interrupts another but then quits before the first one is finished:

 a = [0 1 1 1 1 1 1
 b = [0 0 0 -1 -1 0 0
 r = [0 1 1 -1 -1 ? ?

  Also can 'a' have -1 blocks or 'b' have 1 blocks, and if so how are these to be handled?

Roger Stafford

Subject: How to merge two vectors using special rule?

From: Rob Joy

Date: 31 Dec, 2008 22:53:02

Message: 4 of 8

Hallo Matt,

- There is no situation where -1s and 1s blocks starts at the same position.
- There are only 1s in a
- There are only -1s in b

Thanks Rob

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjgo9c$3g9$1@fred.mathworks.com>...
> "Rob Joy" <joek91@yahoo.com> wrote in message <gjgk1q$juh$1@fred.mathworks.com>...
> > Hallo,
> >
> > I have following Problem:
> >
> > I would like to merge this two vectors. Each new starting block of ones or minus ones in a or b overrides the other already running block of ones or minus ones.
> >
> > a = [0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 ]';
> > b = [0 0 0 0 -1 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 0]';
> >
> > The result schould be like this:
> >
> > result = [0 0 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 1 1 1 1]';
> >
> > Another question is, how to get idx of starting blocks of ones and minus ones in result.
> > Should generate [3 5 10 12 15]
>
> What happens if a block of -1s in "a" starts running at the same point as a block of 1s in "b"? Which overrides the other?

Subject: How to merge two vectors using special rule?

From: Rob Joy

Date: 1 Jan, 2009 00:03:01

Message: 5 of 8

Hallo Roger,

- There are only 1s in a
- There are only -1s in b
- If one block interrupts another but then quits before the first one is finished, interrupted block has no relevancy any more (from the point of interruption) and from this point only the first block that interrupts will be validated.

a = [0 1 1 1 1 1 1
b = [0 0 0 -1 -1 0 0
r = [0 1 1 -1 -1 0 0

Thanks Joe

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjgr6i$rfd$1@fred.mathworks.com>...
> "Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjgo9c$3g9$1@fred.mathworks.com>...
> > "Rob Joy" <joek91@yahoo.com> wrote in message <gjgk1q$juh$1@fred.mathworks.com>...
> > > Hallo,
> > >
> > > I have following Problem:
> > >
> > > I would like to merge this two vectors. Each new starting block of ones or minus ones in a or b overrides the other already running block of ones or minus ones.
> > >
> > > a = [0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 ]';
> > > b = [0 0 0 0 -1 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 0]';
> > >
> > > The result schould be like this:
> > >
> > > result = [0 0 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 1 1 1 1]';
> > >
> > > Another question is, how to get idx of starting blocks of ones and minus ones in result.
> > > Should generate [3 5 10 12 15]
> >
> > What happens if a block of -1s in "a" starts running at the same point as a block of 1s in "b"? Which overrides the other?
>
> Besides Matt's question there is also the matter of how to proceed if one block interrupts another but then quits before the first one is finished:
>
> a = [0 1 1 1 1 1 1
> b = [0 0 0 -1 -1 0 0
> r = [0 1 1 -1 -1 ? ?
>
> Also can 'a' have -1 blocks or 'b' have 1 blocks, and if so how are these to be handled?
>
> Roger Stafford

Subject: How to merge two vectors using special rule?

From: Roger Stafford

Date: 1 Jan, 2009 01:00:04

Message: 6 of 8

"Rob Joy" <joek91@yahoo.com> wrote in message <gjh17l$3nf$1@fred.mathworks.com>...
> ......
> - There are only 1s in a
> - There are only -1s in b
> - If one block interrupts another but then quits before the first one is finished, interrupted block has no relevancy any more (from the point of interruption) and from this point only the first block that interrupts will be validated.
> ......

  Given the nature of your problem, I suspect you have to settle for a for-loop solution. Perhaps something like this:

 n = length(a);
 r = zeros(1,n);
 pa = 0;
 pb = 0;
 f = true;
 for k = 1:n
  if a(k)==1 & pa~=1 % 'a' block starts
   f = true;
  elseif b(k)==-1 & pb~=-1 % 'b' block starts
   f = false;
  end
  if f % Copy from 'a' when f is true and 'b' otherwise
   r(k) = a(k);
  else
   r(k) = b(k);
  end
  pa = a(k); % Save 'a' and 'b' as previous values
  pb = b(k);
 end

  The concept of "go with the most recent block start" pretty much rules out vectorization.

Roger Stafford

Subject: How to merge two vectors using special rule?

From: Matt

Date: 2 Jan, 2009 19:38:02

Message: 7 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjh4ik$59$1@fred.mathworks.com>...

> The concept of "go with the most recent block start"
> pretty much rules out vectorization.


Maybe not! What about as follows?

A=logical(a);
B=logical(b);
dA=diff([0;A])>0; %starts of a-blocks
dB=diff([0;B])>0; %starts of b-blocks

M=dA+2*dB; %Merge

s=cumsum(M);
[h,i,j]=unique(s);
m=nonzeros(M);
if h(1)==0, m=[0;m]; end
inds=m(j); %list of indices

result=a;
result(inds==2)=b(inds==2),

Subject: How to merge two vectors using special rule?

From: Roger Stafford

Date: 4 Jan, 2009 02:47:01

Message: 8 of 8

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjlqeq$qbr$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjh4ik$59$1@fred.mathworks.com>...
>
> > The concept of "go with the most recent block start"
> > pretty much rules out vectorization.
>
> Maybe not! What about as follows?
>
> A=logical(a);
> B=logical(b);
> dA=diff([0;A])>0; %starts of a-blocks
> dB=diff([0;B])>0; %starts of b-blocks
>
> M=dA+2*dB; %Merge
>
> s=cumsum(M);
> [h,i,j]=unique(s);
> m=nonzeros(M);
> if h(1)==0, m=[0;m]; end
> inds=m(j); %list of indices
>
> result=a;
> result(inds==2)=b(inds==2),

  Well done, Matt! I should have known better than to make a claim that Rob's problem "pretty much rules out vectorization." It's the kind of provocative statement that spurs others to strive extra hard to prove it wrong.

Roger Stafford

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