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:
Identifying and counting common elements.

Subject: Identifying and counting common elements.

From: Ulrik Nash

Date: 2 Nov, 2013 18:58:08

Message: 1 of 5

Hi Group,

I have two matrices (A and B) each containing 0's and 1's. All I need is to count the number of places where 1's exist in the same position in the two matrices., but besides looping through, I can't seem to find the solution. I have tried ismember and intersection, but the result was not correct.

For example, in the following
A = [1 0;0 0]
B = [1 1;1 0]

The answer needed is 1.

Subject: Identifying and counting common elements.

From: Roger Stafford

Date: 2 Nov, 2013 19:31:14

Message: 2 of 5

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <l53i00$s98$1@newscl01ah.mathworks.com>...
> I have two matrices (A and B) each containing 0's and 1's. All I need is to count the number of places where 1's exist in the same position in the two matrices .....
- - - - - - - -
 c = sum(sum(A&B));

Roger Stafford

Subject: Identifying and counting common elements.

From: Bruno Luong

Date: 2 Nov, 2013 19:42:14

Message: 3 of 5

Different method:

nnz(A.*B)

% Bruno

Subject: Identifying and counting common elements.

From: Ulrik Nash

Date: 2 Nov, 2013 19:56:15

Message: 4 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <l53kim$5b8$1@newscl01ah.mathworks.com>...
> Different method:
>
> nnz(A.*B)
>
> % Bruno

Thank you very much Bruno! I appreciate it.

Subject: Identifying and counting common elements.

From: Matt J

Date: 3 Nov, 2013 20:48:16

Message: 5 of 5


 sum(A(:) & B(:))

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