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:
union of 2 matrix

Subject: union of 2 matrix

From: ike

Date: 1 Jun, 2012 05:36:23

Message: 1 of 5

i have matrix A
A= 5 9
     7 3
     1 5
     6 7
     7 8
     5 6
     
i have matrix B (the size is longer than matrix A)
B= 1 5
     0 9
     7 8
     1 2
     7 3
     5 9
     2 3
     3 4

if there is a pair of value of A which in B i wanna save it in vector C
so the value of vector C is 3

help me to make it in code
thank you

best regards,
Ike

Subject: union of 2 matrix

From: Nasser M. Abbasi

Date: 1 Jun, 2012 05:57:11

Message: 2 of 5

On 6/1/2012 12:36 AM, ike wrote:
> i have matrix A
> A= 5 9
> 7 3
> 1 5
> 6 7
> 7 8
> 5 6
>
> i have matrix B (the size is longer than matrix A)
> B= 1 5
> 0 9
> 7 8
> 1 2
> 7 3
> 5 9
> 2 3
> 3 4
>
> if there is a pair of value of A which in B i wanna save it in vector C
> so the value of vector C is 3
>
> help me to make it in code
> thank you
>
> best regards,
> Ike


How did you get '3' ?

What do you mean by "save it" ? Save what? What is "it" ?

How is 'C' a vector? 3 is not really a vector, it is a number,
unless you mean [3] ?

Either way, I think all what you want is to use the ismemeber
command:


  ismember(A,B,'rows')

ans =

      1
      1
      1
      0
      1
      0


help ismember for more options.

If you made your question more clear it would have helped.

--Nasser

Subject: union of 2 matrix

From: ike

Date: 1 Jun, 2012 06:11:34

Message: 3 of 5

matrix A under the value
5 9
7 3
1 5

that value was also appear in matrix B

so in C, i wanna count how many value matrix in A appear in matrix B

that's my mean

sorry if it's so difficult to understand

Subject: union of 2 matrix

From: Nasser M. Abbasi

Date: 1 Jun, 2012 06:18:59

Message: 4 of 5

On 6/1/2012 1:11 AM, ike wrote:
> matrix A under the value
> 5 9
> 7 3
> 1 5
>
> that value was also appear in matrix B
>
> so in C, i wanna count how many value matrix in A appear in matrix B
>
> that's my mean
>
> sorry if it's so difficult to understand

You are missing

     7 8

this row is in A and in B, so why not count it? So the
count is 4 not 3 as you said.

I think all what you need is ismember

---------------------------------
  sum(ismember(A,B,'rows'))
----------------------------------

4

But note, this counts all rows in A that in B also. Even
if the row is duplicated in A.


--Nasser

Subject: union of 2 matrix

From: Steven_Lord

Date: 1 Jun, 2012 13:28:21

Message: 5 of 5



"ike " <ike.marsa@gmail.com> wrote in message
news:jq9mem$6ps$1@newscl01ah.mathworks.com...
> matrix A under the value
> 5 9
> 7 3
> 1 5
>
> that value was also appear in matrix B
>
> so in C, i wanna count how many value matrix in A appear in matrix B

So you want to know which rows are in _both_ A AND B? That's not the union
(which would be the list of all rows that are in _either_ A OR B) but the
intersection of A and B, which you can compute with the INTERSECT function.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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