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:
compare two vectors

Subject: compare two vectors

From: ann au

Date: 2 Nov, 2008 00:45:03

Message: 1 of 7

Hi,

I have two vectors:

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

I want to get the following result:
r = [1 1 1 3 4 7]'

What code can I use?
Thanks a lot!

Ann

Subject: compare two vectors

From: Image Analyst

Date: 2 Nov, 2008 02:38:02

Message: 2 of 7

"ann au" <fannylyy@hkusua.hku.hk> wrote in message <geit6f$8ev$1@fred.mathworks.com>...
> Hi,
>
> I have two vectors:
>
> a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> b = [1 1 2 2 5 6]'
>
> I want to get the following result:
> r = [1 1 1 3 4 7]'
>
> What code can I use?
> Thanks a lot!
>
> Ann
-----------------------------------------------------
Not exactly sure what you're after, or what algorithm you're using, but this will work ;-)

a=[1 1 1 1 1 2 2 3 4 5 6 7]'
b = [1 1 2 2 5 6]'
r = CompareVectors(a, b);

function returnArray=CompareVectors(a, b)
    returnArray = [1 1 1 3 4 7];
    return;

Subject: compare two vectors

From: Husam Aldahiyat

Date: 2 Nov, 2008 04:16:02

Message: 3 of 7

I think it's a riddle.

Subject: compare two vectors

From: Seth

Date: 2 Nov, 2008 05:48:02

Message: 4 of 7

Maybe you are looking for something like this that returns the dissimilar elements?

a=[1 1 1 1 1 2 2 3 4 5 6 7]';
b = [1 1 2 2 5 6]';
r = zeros(1,length(a)-length(b));
r_c = 1;

for i = 1:max(a)
    k = (sum(a==i) - sum(b==i));
    if k > 0
        for j = 1:k
            r(r_c) = i;
            r_c = r_c + 1;
        end
    end
end

Seth

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <gej3qa$609$1@fred.mathworks.com>...
> "ann au" <fannylyy@hkusua.hku.hk> wrote in message <geit6f$8ev$1@fred.mathworks.com>...
> > Hi,
> >
> > I have two vectors:
> >
> > a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> > b = [1 1 2 2 5 6]'
> >
> > I want to get the following result:
> > r = [1 1 1 3 4 7]'
> >
> > What code can I use?
> > Thanks a lot!
> >
> > Ann
> -----------------------------------------------------
> Not exactly sure what you're after, or what algorithm you're using, but this will work ;-)
>
> a=[1 1 1 1 1 2 2 3 4 5 6 7]'
> b = [1 1 2 2 5 6]'
> r = CompareVectors(a, b);
>
> function returnArray=CompareVectors(a, b)
> returnArray = [1 1 1 3 4 7];
> return;

Subject: compare two vectors

From: Walter Roberson

Date: 2 Nov, 2008 06:04:48

Message: 5 of 7

Husam Aldahiyat wrote:
> I think it's a riddle.

No, it's just the multi-set difference.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: compare two vectors

From: Roger Stafford

Date: 2 Nov, 2008 08:14:02

Message: 6 of 7

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

  I assume that for each element present in 'a' in greater quantity than in 'b' you wish 'r' to contain as many copies of that element as this difference. That is, in your example 'r' has three 1's presumably because 'a' contains three more 1's than does 'b'.

  If that is what you intend, try this:

 e = union(a,b); % List all unique elements of combined a and b
 n = max(histc(a,e)-histc(b,e),0); % How many of each e element
 p = accumarray(cumsum(n)+1,1); % Proceed to generate r
 p(1) = p(1)+1;
 r = e(cumsum(p(1:end-1)));

  The vector 'n' above contains the desired number of elements for 'r' of each of the corresponding elements of 'e'. The remaining three lines are devoted to generating just such a vector 'r'.

Roger Stafford

Subject: compare two vectors

From: Bruno Luong

Date: 2 Nov, 2008 12:43:02

Message: 7 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gejnga$3p9$1@fred.mathworks.com>...
> The remaining three lines are devoted to generating just such a vector 'r'.
>

These "remaining" three lines are quite clever.

I would suggest a more naive alternative for these three lines:

r=arrayfun(@(ek,nk) repmat(ek,nk,1),e,n,...
                   'UniformOutput',false)
r = cat(1,r{:})

And here is another approach:

e=union(a,b);
[trast ia]=ismember(a,e);
[trast ib]=ismember(b,e);
count = @(i) {1:length(i)};
ca = accumarray(ia(:,1), (1:length(a))', [], count);
cb = accumarray(ib(:,1), (1:length(b))', [], count);
ia(:,2)=cat(2,ca{:});
ib(:,2)=cat(2,cb{:});

r = a(~ismember(ia,ib,'rows'))

% Bruno

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