Thread Subject: Help with a tricky Vector Problem

Subject: Help with a tricky Vector Problem

From: ade77

Date: 13 Nov, 2009 16:54:19

Message: 1 of 4

Hi


I am trying to manipulate a large vector without a loop.

For example
a = [1:10], b = [1 2 4 5 8 10]

a b

1 1
2 2
3 4
4 5
5 8
6 10
7
8
9
10

I want all the elements of 'a' to be in in 'b'. At the end I want 'b' to be b= 1:10

1. both vectors are already sorted
2. any element of 'a' not in 'b', should be added to b, and the index noted.
3. for the example above, first index of 'a' is 1, it is in 'b',next, second index of 'a' is 2, it is in 'b'.
4.third index of 'a' is 3, not in 'b', hence 3 will be added to b, and the index noted (3).
5. Note now, that b is now [1 2 3 4 5 8 10].
6 Next,'a' at index 4 is 4, which is also in index 4 of b, then continue to index 5 of 'a' e t c.

at the end b = [1:10], index = 3,6,7,9.

  

Subject: Help with a tricky Vector Problem

From: Matt Fetterman

Date: 13 Nov, 2009 17:00:25

Message: 2 of 4

"ade77 " <ade100a@gmail.com> wrote in message <hdk2vr$i5k$1@fred.mathworks.com>...
> Hi
>
>
> I am trying to manipulate a large vector without a loop.
>
> For example
> a = [1:10], b = [1 2 4 5 8 10]
>
> a b
>
> 1 1
> 2 2
> 3 4
> 4 5
> 5 8
> 6 10
> 7
> 8
> 9
> 10
>
> I want all the elements of 'a' to be in in 'b'. At the end I want 'b' to be b= 1:10
>
> 1. both vectors are already sorted
> 2. any element of 'a' not in 'b', should be added to b, and the index noted.
> 3. for the example above, first index of 'a' is 1, it is in 'b',next, second index of 'a' is 2, it is in 'b'.
> 4.third index of 'a' is 3, not in 'b', hence 3 will be added to b, and the index noted (3).
> 5. Note now, that b is now [1 2 3 4 5 8 10].
> 6 Next,'a' at index 4 is 4, which is also in index 4 of b, then continue to index 5 of 'a' e t c.
>
> at the end b = [1:10], index = 3,6,7,9.
>
>

Actually not that tricky.
a=[1:10];
 b = [1 2 4 5 8 10];
NewVec=sort(unique([a b]) );
IndexVec=find(~ismember(NewVec, b) );
Regards Matt

Subject: Help with a tricky Vector Problem

From: Matt

Date: 13 Nov, 2009 19:05:20

Message: 3 of 4


index=setdiff(a,b);
b=union(a,b);

Subject: Help with a tricky Vector Problem

From: ade77

Date: 13 Nov, 2009 19:16:03

Message: 4 of 4

"Matt " <xys@whatever.com> wrote in message <hdkalg$gjc$1@fred.mathworks.com>...
>
> index=setdiff(a,b);
> b=union(a,b);

Thank you Matt

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com