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:
'unique' problem

Subject: 'unique' problem

From: proecsm

Date: 29 Oct, 2010 00:36:03

Message: 1 of 4

I am using the unique function to determine if there are repeated digits in a 6 digit number, but I am only interested in a certain number of repeated digits (say 3).

If I do length(unique('999763')), I get 4 and if I do length(unique('998551')) I also get 4, but I only care about the first case.

So my questions are: can I check for repeated digits without converting to strings and once I separate the cases that are length 4, is there a 'elegant' way to separate cases of 3 repeated digits versus 2 pairs of 2 repeated digits? thanks.

Subject: 'unique' problem

From: Darren Rowland

Date: 29 Oct, 2010 01:28:04

Message: 2 of 4

I'll just point out a better method to determine whether a particular digit occurs 3 times. Your method may fail in some cases like the third example below

find(histc('999763','0123456789')==3)-1 % ans = [9]
find(histc('998551','0123456789')==3)-1 % ans = []
find(histc('111444','0123456789')==3)-1 % ans = [1,4]

Hth
Darren

Subject: 'unique' problem

From: Jan Simon

Date: 29 Oct, 2010 10:27:03

Message: 3 of 4

Dear proecsm,

another method:
  hasMoreThan3 = (max(accumarray(transpose('999763') - ('0' - 1), 1)) > 3);

Jan

Subject: 'unique' problem

From: proecsm

Date: 29 Oct, 2010 22:42:04

Message: 4 of 4

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iae7hn$8ah$1@fred.mathworks.com>...
> Dear proecsm,
>
> another method:
> hasMoreThan3 = (max(accumarray(transpose('999763') - ('0' - 1), 1)) > 3);
>
> Jan

Thanks Jan & Darren! I didn't realise that histc worked with strings, it looks like that approach will work best for me. Thanks again.

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