Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Same elements in a matrix

Subject: Same elements in a matrix

From: Shani Gal

Date: 1 Jun, 2008 05:31:01

Message: 1 of 5

Hi
Lets say I have a matrix n_by_2

I would like to find the rows in the matrix which have the
same elements

 For example, if this is part of the matrix, I need to
know that row 1 is the same as row 5 AND the same as row
3 !.

 How can I do this?

21 30
12 50
30 21
25 85
21 30

Thank you

Shani

Subject: Same elements in a matrix

From: us

Date: 1 Jun, 2008 09:38:02

Message: 2 of 5

"Shani Gal":
<SNIP uniqueness-evergreen...

> I would like to find the rows in the matrix which have
the same elements...

a hint:

     help unique;
% peruse the three output args!

us

Subject: Same elements in a matrix

From: Greg Heath

Date: 1 Jun, 2008 13:46:50

Message: 3 of 5

On Jun 1, 5:38=A0am, "us " <u...@neurol.unizh.ch> wrote:
> "Shani Gal":
> <SNIP uniqueness-evergreen...
>
> > I would like to find the rows in the matrix which have
>
> the same elements...
>
> a hint:
>
> =A0 =A0 =A0help unique;
> % peruse the three output args!

That won't work if each element has to have
the same number of occurences. For example,
does the OP accept these ?

1 2 3 1
1 2 3 2
1 2 3 3

If not, then use SORT.

Hope this help.

Greg

Subject: Same elements in a matrix

From: us

Date: 1 Jun, 2008 15:57:03

Message: 4 of 5

Greg Heath:
<SNIP down to cautionary remark...

> If not, then use SORT...

yes - i know i should have provided the (usual) example...

     m=[
          21 30
          12 50
          30 21
          25 85
          21 30
     ];
     [r,r,r]=unique(sort(m,2,'ascend'),'rows');
     disp([r,m]);
%{
     note: indexing due to sorting in UNIQUE(!)
     r .mat.
     2 21 30 % <-
     1 12 50
     2 30 21 % <-
     3 25 85
     2 21 30 % <-
%}

us

Subject: Same elements in a matrix

From: Scott

Date: 1 Jun, 2008 16:55:03

Message: 5 of 5

"Shani Gal" <shanigal5@walla.co.il> wrote in message
<g1tc6l$3kt$1@fred.mathworks.com>...
> Hi
> Lets say I have a matrix n_by_2
>
> I would like to find the rows in the matrix which have
the
> same elements
>
> For example, if this is part of the matrix, I need to
> know that row 1 is the same as row 5 AND the same as row
> 3 !.
>
> How can I do this?
>
> 21 30
> 12 50
> 30 21
> 25 85
> 21 30
>
> Thank you
>
> Shani
>

Shani,

Other might be able to do this with more compact syntax and
perhaps without the loop but it works:

A = [21 30
    12 50
    30 21
    25 85
    21 30]
[B,I,J] = unique(A,'rows');
d = setdiff(J, I);
c = setdiff(I, J);
rows_common = [d, c];
M = fliplr(A);
n = A(d,:);
sz = size(A);
for i = 1:sz(1)
    [rows cols v] = find(M(i,:)==n);
    if ~isempty(rows) & rows(1,1:2) == [1 1];
        rows_common = [rows_common i];
    end
end
rows_common = sort(rows_common)
% end of code


The output is:

A =
    21 30
    12 50
    30 21
    25 85
    21 30
rows_common =
     1 3 5

hth,
Scott

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sort us 1 Jun, 2008 12:00:25
code us 1 Jun, 2008 12:00:25
unique us 1 Jun, 2008 05:40:07
evergreen us 1 Jun, 2008 05:40:07
reference us 1 Jun, 2008 05:40:07
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics