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 judge two matrixes containing the same element and delete them in one matrix?

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: dai

Date: 19 May, 2009 11:21:02

Message: 1 of 8

Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: us

Date: 19 May, 2009 11:29:02

Message: 2 of 8

"dai " <279711585@qq.com> wrote in message <guu4mu$ler$1@fred.mathworks.com>...
> Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.

a hint:

     help ismember; % and other membership functions...

us

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: Nicolaie Popescu-Bodorin

Date: 19 May, 2009 11:42:01

Message: 3 of 8

Hello dai,

here is your code:

a = [1 2 3 4 5]; b = [4 5 6 7 8];
acopy=a; bma=b;
while ~isempty(acopy),
bma = bma(bma~¬opy(1));
acopy¬opy(acopy~¬opy(1));
end;
disp(bma)

If it make sens in your application, be aware of NaN elements appearing in a or in b!

Regards,
Bodorin


"dai " <279711585@qq.com> wrote in message <guu4mu$ler$1@fred.mathworks.com>...
> Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: Nicolaie Popescu-Bodorin

Date: 19 May, 2009 11:51:02

Message: 4 of 8

"Nicolaie Popescu-Bodorin" <freeseosubmit@yah00.com> wrote in message <guu5u9$akb$1@fred.mathworks.com>...
Hello dai,

for some unknown reasons the previous reply isn't so clear (some characters are missing)
your code again:
 
a = [1 2 3 4 5]; b = [4 5 6 7 8];
acopy=a; bma=b;
while ~isempty(acopy),
bma = bma(bma ~= acopy(1));
acopy = acopy(acopy ~= acopy(1));
end;
disp(bma)

Regards,
Bodorin


> "dai " <279711585@qq.com> wrote in message <guu4mu$ler$1@fred.mathworks.com>...
> > Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: us

Date: 19 May, 2009 12:08:02

Message: 5 of 8

"Nicolaie Popescu-Bodorin" <freeseosubmit@yah00.com> wrote in message <guu6f6$ebg$1@fred.mathworks.com>...
> "Nicolaie Popescu-Bodorin" <freeseosubmit@yah00.com> wrote in message <guu5u9$akb$1@fred.mathworks.com>...
> Hello dai,
>
> for some unknown reasons the previous reply isn't so clear (some characters are missing)
> your code again:
>
> a = [1 2 3 4 5]; b = [4 5 6 7 8];
> acopy=a; bma=b;
> while ~isempty(acopy),
> bma = bma(bma ~= acopy(1));
> acopy = acopy(acopy ~= acopy(1));
> end;
> disp(bma)
>
> Regards,
> Bodorin

or - as was mentioned earlier -

     a=1:5;
     b=4:8;
     ix=ismember(b,a);
     r=b(~ix)
% r = 6 7 8
     r=setdiff(b,a)
% r = 6 7 8

us

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: Steven Lord

Date: 19 May, 2009 13:22:39

Message: 6 of 8


"us " <us@neurol.unizh.ch> wrote in message
news:guu7f2$i8p$1@fred.mathworks.com...
> "Nicolaie Popescu-Bodorin" <freeseosubmit@yah00.com> wrote in message
> <guu6f6$ebg$1@fred.mathworks.com>...
>> "Nicolaie Popescu-Bodorin" <freeseosubmit@yah00.com> wrote in message
>> <guu5u9$akb$1@fred.mathworks.com>...
>> Hello dai,
>>
>> for some unknown reasons the previous reply isn't so clear (some
>> characters are missing)
>> your code again:
>>
>> a = [1 2 3 4 5]; b = [4 5 6 7 8];
>> acopy=a; bma=b;
>> while ~isempty(acopy),
>> bma = bma(bma ~= acopy(1));
>> acopy = acopy(acopy ~= acopy(1));
>> end;
>> disp(bma)
>>
>> Regards,
>> Bodorin
>
> or - as was mentioned earlier -
>
> a=1:5;
> b=4:8;
> ix=ismember(b,a);
> r=b(~ix)
> % r = 6 7 8
> r=setdiff(b,a)
> % r = 6 7 8
>
> us

I think the OP's description had an error -- if we follow the procedure they
described in words, the result should be [1 2 3 6 7 8], but they said they
wanted [6 7 8]. If the expected result was incorrect and the correct answer
they expect is [1:3 6:8], we can do that in one step:


a = 1:5;
b = 4:8;
combinedData = setxor(a, b)


If not, the OP should restate what they're trying to do more clearly.

--
Steve Lord
slord@mathworks.com

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: dai

Date: 20 May, 2009 02:47:02

Message: 7 of 8

"dai " <279711585@qq.com> wrote in message <guu4mu$ler$1@fred.mathworks.com>...
> Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.

All of above are very helpful for me. But I still want to know another problem. That is is a = [1 2;3 4], I wish to search the index of a given rowin the matrix. I type find(a=[3 4]), but it turned out to be wrong. So what can I do next?

Subject: How to judge two matrixes containing the same element and delete them in one matrix?

From: Bruno Luong

Date: 20 May, 2009 07:10:17

Message: 8 of 8

"dai " <279711585@qq.com> wrote in message <guvqv6$kma$1@fred.mathworks.com>...
> "dai " <279711585@qq.com> wrote in message <guu4mu$ler$1@fred.mathworks.com>...
> > Hi, guys. I have coming to a problem. Since I am not familiar with the functions in the matlab, I can not solve this problem which is how to judge two matrixes containing the same element. For example, I have a = [1 2 3 4 5] and b = [4 5 6 7 8]. And now I wish to delete the same elements in a from b and get [6 7 8]. Please give me one hand.
>
> All of above are very helpful for me. But I still want to know another problem. That is is a = [1 2;3 4], I wish to search the index of a given rowin the matrix. I type find(a=[3 4]), but it turned out to be wrong. So what can I do next?

Use ISMEMBER with 'ROWS' option.

If your data are float (double or single class with fractional different zero), it is recommended not to use exact comparison. There is a ISMEMBERF in FEX

http://www.mathworks.com/matlabcentral/fileexchange/23049

>> ismemberf([1 2; 3 4],[3 4]+eps,'rows')

ans =

     0
     1

>>

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