Got Questions? Get Answers.
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:
return the rows where no elements are repeated in multiple rows

Subject: return the rows where no elements are repeated in multiple rows

From: audley james

Date: 16 Oct, 2010 03:05:05

Message: 1 of 11

Hi,

If I have matrix X:

X =

     1 4
     3 6
     6 11
     7 8
     7 9
     7 10
     9 10

How can I return the rows where no elements are repeated in multiple rows ?

Such as :

return =
                  1 4
                  3 6
                  7 8
                  9 10

Thanks
audley

Subject: return the rows where no elements are repeated in multiple rows

From: Oleg Komarov

Date: 16 Oct, 2010 03:53:04

Message: 2 of 11

"audley james" <audleyer@gmail.com> wrote in message <i9b4p1$kgj$1@fred.mathworks.com>...
> Hi,
>
> If I have matrix X:
>
> X =
>
> 1 4
> 3 6
> 6 11
> 7 8
> 7 9
> 7 10
> 9 10
>
> How can I return the rows where no elements are repeated in multiple rows ?
>
> Such as :
>
> return =
> 1 4
> 3 6
> 7 8
> 9 10
>
> Thanks
> audley

Use unique on the first column.

Oleg

Subject: return the rows where no elements are repeated in multiple rows

From: audley james

Date: 16 Oct, 2010 04:02:05

Message: 3 of 11

Oleg,

thanks for response but it doesnt seem to work.

I get the following:

 unique(X(:,1))

ans =

     1
     3
     6
     7
     9


But instead I'm ideally trying got get ,

ans =

                  1
                  3
                  7
                  9

Subject: return the rows where no elements are repeated in multiple rows

From: Bruno Luong

Date: 16 Oct, 2010 06:52:04

Message: 4 of 11

"audley james" <audleyer@gmail.com> wrote in message <i9b4p1$kgj$1@fred.mathworks.com>...
> Hi,
>
> If I have matrix X:
>
> X =
>
> 1 4
> 3 6
> 6 11
> 7 8
> 7 9
> 7 10
> 9 10
>
> How can I return the rows where no elements are repeated in multiple rows ?
>
> Such as :
>
> return =
> 1 4
> 3 6
> 7 8
> 9 10

As I understand this works as well:

1 4
6 11
7 10

Why the first is preferred? Is there a randomness is the problem statement?

Bruno

Subject: return the rows where no elements are repeated in multiple rows

From: audley james

Date: 16 Oct, 2010 07:10:05

Message: 5 of 11

Bruno,

I didnt notice that, thanks. Any one of the 2 is desired.

Audley

Subject: return the rows where no elements are repeated in multiple rows

From: Roger Stafford

Date: 16 Oct, 2010 07:23:06

Message: 6 of 11

"audley james" <audleyer@gmail.com> wrote in message <i9b4p1$kgj$1@fred.mathworks.com>...
> Hi,
>
> If I have matrix X:
>
> X =
>
> 1 4
> 3 6
> 6 11
> 7 8
> 7 9
> 7 10
> 9 10
>
> How can I return the rows where no elements are repeated in multiple rows ?
>
> Such as :
>
> return =
> 1 4
> 3 6
> 7 8
> 9 10
>
> Thanks
> audley
- - - - - - - - -
  With X an n by 2 array, do this:

 n = size(X,1);
 [~,p,q] = unique(reshape(X.',1,[]),'first');
 t = reshape(p(q),2,[]);
 t1 = t(1,:); t2 = t(2,:);
 s1 = 1:2:2*n; s2 = 2:2:2*n;
 Y = X(t1==s1&(t2==s1|t2==s2),:);

  There is a question of how you wish to deal with a row in which both elements are equal but this is the first row that value has occurred. I have assumed above that you would keep it. If you wish to discard such row, the last line should be changed to:

 Y = X(t1==s1&t2==s2,:);

Roger Stafford

Subject: return the rows where no elements are repeated in multiple rows

From: Bruno Luong

Date: 16 Oct, 2010 07:34:04

Message: 7 of 11

Try this:

X = [ 1 4
3 6
6 11
7 8
7 9
7 10
9 10]


[~, I, J] = unique(X(:,1))
[~, I, ~] = unique(X(I(J),2))
U = X(I,:)

% Bruno

Subject: return the rows where no elements are repeated in multiple rows

From: Roger Stafford

Date: 16 Oct, 2010 07:38:03

Message: 8 of 11

"audley james" <audleyer@gmail.com> wrote in message <i9bj4d$k29$1@fred.mathworks.com>...
> Bruno,
> I didnt notice that, thanks. Any one of the 2 is desired.
> Audley
- - - - - - - - - -
  In the post I sent before this one, I assumed that you meant you would preserve any row provided that neither of its elements had occurred in an earlier row. It's the only way I could arrive at a unique algorithm, and your example was in accord with this notion. Your reply to Bruno suggests that perhaps that assumption was not correct. If not, how do you choose between two rows which have an element in common?

Roger Stafford

Subject: return the rows where no elements are repeated in multiple rows

From: audley james

Date: 16 Oct, 2010 07:44:04

Message: 9 of 11

 Well, thanks again roger,

But I'm getting:

                   1 4
                   3 6
                   7 8

Not :

                   1 4
                   3 6
                   7 8
                   9 10

I'm trying to understand your code but just not seeing where this miss may be.

Audley

Subject: return the rows where no elements are repeated in multiple rows

From: Bruno Luong

Date: 16 Oct, 2010 09:01:09

Message: 10 of 11

If you want to avoid repeat elements to appeared in cross column as well, then this should do:

X = [ 1 4
    3 6
    6 11
    7 8
    7 9
    7 10
    9 10]

[U, ~, XX] = unique(X);
mark = false(size(U));
keep = false(1,size(X,1));
XX = reshape(XX, size(X));
for k=1:size(X,1)
    if ~any(mark (XX(k,:)))
        keep(k) = true;
        mark (XX(k,:)) = true;
    end
end
U = X(keep,:)

% Bruno

Subject: return the rows where no elements are repeated in multiple rows

From: Roger Stafford

Date: 16 Oct, 2010 10:34:08

Message: 11 of 11

"audley james" <audleyer@gmail.com> wrote in message <i9bl44$q92$1@fred.mathworks.com>...
> Well, thanks again roger,
>
> But I'm getting:
>
> 1 4
> 3 6
> 7 8
>
> Not :
>
> 1 4
> 3 6
> 7 8
> 9 10
>
> I'm trying to understand your code but just not seeing where this miss may be.
>
> Audley
- - - - - - -
  The (9,10) row was not included because a 10 was present in the preceding row even though that row was also left out. That is a basic defect in my algorithm.

  Bruno has given a solution using a for-loop which avoids this difficulty. Remarkably enough, in seeking a remedy I arrived at almost an identical solution to Bruno's (which I therefore will not post) before I looked at his, which strengthens my suspicion that a for-loop is inherently necessary in this problem.

Roger Stafford

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