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:
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

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