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:
Remove rows with repeated numbers except for zeros

Subject: Remove rows with repeated numbers except for zeros

From: Philipp

Date: 8 Jun, 2011 15:35:11

Message: 1 of 6

Hi,
how can i remove rows with repeated digits in A in order to form new_A except for rows where the repeated digit is zero?

A =
     72 81 33
     51 67 28
     33 33 42
     25 48 25
     0 0 98
     
        
new_A =

    72 81 33
    51 67 28
    25 24 0
    0 0 98

In a previous thread Nasser answered with: A(find(arrayfun(@(i)length(unique(A(i,:))),1:size(A,1))==size(A,2)),:)

But this also deletes rows which contain repeated zeros.

Thanks for your help!

Philipp

Subject: Remove rows with repeated numbers except for zeros

From: ImageAnalyst

Date: 8 Jun, 2011 16:59:13

Message: 2 of 6

How did the row
25 48 25
turn into
25 24 0?

Subject: Remove rows with repeated numbers except for zeros

From: Roger Stafford

Date: 8 Jun, 2011 22:49:04

Message: 3 of 6

"Philipp" wrote in message <iso4re$5mi$1@newscl01ah.mathworks.com>...
> how can i remove rows with repeated digits in A in order to form new_A except for rows where the repeated digit is zero?
>
> A =
> 72 81 33
> 51 67 28
> 33 33 42
> 25 48 25
> 0 0 98
>
> new_A =
> 72 81 33
> 51 67 28
> 25 24 0
> 0 0 98
> ..........
- - - - - - - - - -
  I am going by your words, not your apparently erroneous example:

 t = sort(A,2);
 p = all(diff(t,1,2)~=0|t(:,1:end-1)==0,2);
 new_A = A(p,:);

  Note: With this code if a row has non-zero repeated numbers, it will be deleted even if it also possesses some repeated zeros. I assume that is what you want. Also I assume by 'repeated' you mean to include equal numbers which are not necessarily contiguous in the row.

Roger Stafford

Subject: Remove rows with repeated numbers except for zeros

From: Philipp

Date: 9 Jun, 2011 17:19:05

Message: 4 of 6

> I am going by your words, not your apparently erroneous example:
>
> t = sort(A,2);
> p = all(diff(t,1,2)~=0|t(:,1:end-1)==0,2);
> new_A = A(p,:);
>
> Note: With this code if a row has non-zero repeated numbers, it will be deleted even if it also possesses some repeated zeros. I assume that is what you want. Also I assume by 'repeated' you mean to include equal numbers which are not necessarily contiguous in the row.
>
> Roger Stafford

Sorry guys for the erroneous example. Of course the row 25 48 25 should be removed and not turn into 25 24 0. My concentration was down after trying solve the problem.

@Roger: Your assumptions are absolutely correct and your code does exactly what I want. I really don't know how you geniuses are able to code the solution of such a problem in only 3 lines.

Thank you very much!

Subject: Remove rows with repeated numbers except for zeros

From: Steven_Lord

Date: 9 Jun, 2011 17:37:53

Message: 5 of 6



"Philipp " <phil-mayer@web.de> wrote in message
news:isqva8$84s$1@newscl01ah.mathworks.com...

*snip*

> @Roger: Your assumptions are absolutely correct and your code does exactly
> what I want. I really don't know how you geniuses are able to code the
> solution of such a problem in only 3 lines.

Practice, practice, and even more practice.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Remove rows with repeated numbers except for zeros

From: Roger Stafford

Date: 9 Jun, 2011 17:56:04

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <isr0dh$bjg$1@newscl01ah.mathworks.com>...
> "Philipp " <phil-mayer@web.de> wrote in message
> news:isqva8$84s$1@newscl01ah.mathworks.com...
> > @Roger: Your assumptions are absolutely correct and your code does exactly
> > what I want. I really don't know how you geniuses are able to code the
> > solution of such a problem in only 3 lines.
> Practice, practice, and even more practice.
> Steve Lord
- - - - - - - - - -
  Awww, you spoiled it, Steve. Just for a tiny instant there I was a "genius". ;-)

  Actually it could have been squeezed down to just two lines:

 t = sort(A,2);
 new_A = A(all(diff(t,1,2)~=0|t(:,1:end-1)==0,2),:);

but it becomes even harder to decipher.

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