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 compare these two vectors?

Subject: how to compare these two vectors?

From: ann au

Date: 2 Nov, 2008 00:42:02

Message: 1 of 5

Hi,

I have 2 vectors:
a=[1 1 1 1 1 2 2 3 4 5 6 7]'
b = [1 1 1 2 2 3 5 7]'

I want to get the following result:
a=[ 1 1 4 6 ]'

What code should i use? Thanks!

Ann

Subject: how to compare these two vectors?

From: Wolfgang Schwanghart

Date: 2 Nov, 2008 10:03:02

Message: 2 of 5

Hi Ann,

it took me a while to figure it out and I am sure, there are better solutions. So hope this works for you...

n = max(max(a),max(b));
e = accumarray(a,ones(size(a)),[n 1]) - accumarray(b,ones(size(b)),[n 1]);
g = [1:n]';
f = g(find(e));
e = e(f);
ix = cellfun(@(x,y) repmat(x,y,1),num2cell(f),num2cell(e),'uniformoutput',false);
ix = cell2mat(ix)

ix =

     1
     1
     4
     6

HTH
Wolfgang


"ann au" <fannylyy@hkusua.hku.hk> wrote in message <geit0p$716$1@fred.mathworks.com>...
> Hi,
>
> I have 2 vectors:
> a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> b = [1 1 1 2 2 3 5 7]'
>
> I want to get the following result:
> a=[ 1 1 4 6 ]'
>
> What code should i use? Thanks!
>
> Ann

Subject: how to compare these two vectors?

From: Jos

Date: 2 Nov, 2008 20:35:03

Message: 3 of 5

"ann au" <fannylyy@hkusua.hku.hk> wrote in message <geit0p$716$1@fred.mathworks.com>...
> Hi,
>
> I have 2 vectors:
> a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> b = [1 1 1 2 2 3 5 7]'
>
> I want to get the following result:
> a=[ 1 1 4 6 ]'
>
> What code should i use? Thanks!
>
> Ann

This should give you a start:

a=[1 1 1 1 1 2 2 3 4 5 6 7]
b = [1 1 1 2 2 3 5 7]

% step 1 : get remaining elements
[uab] = union(a,b)
n = histc(a,uab) - histc(b,uab)
uab = uab(n>0)
n = n(n>0)

% step 2: look for runlength decoding algorithms on the file exchange

hth
Jos

Subject: how to compare these two vectors?

From: Jan Simon

Date: 5 Nov, 2008 15:58:02

Message: 4 of 5

Dear Ann!

> I have 2 vectors:
> a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> b = [1 1 1 2 2 3 5 7]'
> I want to get the following result:
> a=[ 1 1 4 6 ]'

You want to remove elements of [b] from [a] considering repetitions?
If 0 does never appear in [a], try:

for bi = 1:length(b)
  a(find(a == b(bi), 1)) = 0;
end
result = a(a ~= 0)

Sometimes loops are easy enough to be fast.
Kind regards, Jan

Subject: how to compare these two vectors?

From: Sprinceana

Date: 6 May, 2009 19:42:02

Message: 5 of 5

Thanks to everybody!

I used also setdiff and it works perfectly for my vectors:

>> a=[1 4 7 10]

a =

     1 4 7 10

>> b=[7 10]

b =

     7 10

>> c=setdiff(a,b)

c =

     1 4

>>

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