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:
find common values

Subject: find common values

From: George34 Dampf

Date: 13 Nov, 2010 19:36:06

Message: 1 of 6

Hi,

I have a large list (nx3) containg three values.

Now I look at the first row (e.g. [1 131 2])
and I want to find all rows that have at least two values in common
(e.g. [131 2 99] or [2 1 44])...

Is there a simple solution?

Subject: find common values

From: Matt J

Date: 13 Nov, 2010 20:01:05

Message: 2 of 6


Rows=find( sum( ismember(YourNx3Matrix,[1 131 2]) , 2)>=2 )

Subject: find common values

From: Roger Stafford

Date: 13 Nov, 2010 22:17:03

Message: 3 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ibmqq1$dfj$1@fred.mathworks.com>...
>
> Rows=find( sum( ismember(YourNx3Matrix,[1 131 2]) , 2)>=2 )
- - - - - - -
  If George wants this comparison to work even if the given row or other rows have repeated values, then this wouldn't work. The best I can think of at the moment is to prepare in advance all six permutations of the given row and count the number of equalities in each match with a row to be tested. George had better tell us if this is needed.

Roger Stafford

Subject: find common values

From: Bruno Luong

Date: 14 Nov, 2010 06:32:03

Message: 4 of 6

For duplicated elements issue, what about the below code? The idea is count the number of elements that match a unique representation of the first row, then clip it when this number is larger than the count for the first row.

% Data
A=ceil(4*rand(20,3))

% Engine
[m n] = size(A);
u = unique(A(1,:)); % representation of first row
[in J] = ismember(A,u);
r = repmat((1:m)', n, 1);
c = accumarray([r(in) J(in)],1); % count
c = bsxfun(@min,c,c(1,:)); % clip
rows = find(sum(c,2)==2);

% Check
rows
A(1,:)
A(rows,:)

% Bruno

Subject: find common values

From: Roger Stafford

Date: 14 Nov, 2010 12:39:04

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibnvp3$b0c$1@fred.mathworks.com>...
> For duplicated elements issue, what about the below code? The idea is count the number of elements that match a unique representation of the first row, then clip it when this number is larger than the count for the first row.
>
> % Data
> A=ceil(4*rand(20,3))
>
> % Engine
> [m n] = size(A);
> u = unique(A(1,:)); % representation of first row
> [in J] = ismember(A,u);
> r = repmat((1:m)', n, 1);
> c = accumarray([r(in) J(in)],1); % count
> c = bsxfun(@min,c,c(1,:)); % clip
> rows = find(sum(c,2)==2);
>
> % Check
> rows
> A(1,:)
> A(rows,:)
>
> % Bruno
- - - - - - - - -
  Very good, Bruno! That would handle the general case for any n. In effect you are getting a histogram of each row but with counts restricted to the values that occur in the first row. Then as you say, each histogram is "clipped" to the histogram of the first row itself. In each row that would give you the best match among possible one-to-one mappings.

  One minor correction, though. The last line should read

 rows = find(sum(c,2)>=2);

to be in accordance with the "at least" in the statement "at least two values in common".

Roger Stafford

Subject: find common values

From: HollandFelicia

Date: 17 Dec, 2010 21:54:19

Message: 6 of 6

Different people in every country get the <a href="http://bestfinance-blog.com">loan</a> from different creditors, just because this is comfortable and fast.

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