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:
random number generator

Subject: random number generator

From: Wendy

Date: 26 Feb, 2010 14:24:04

Message: 1 of 5

Hi all,

I am generating a vector using rand function. Is there a function that I can check if there is any repeated number, instead of checking element by element? My example is as follow,

C = textscan(fid,'%s %s');
if size(C{1},1)<3;
    n = 1;
else
    n = round(size(C{1},1)*(1/3)); %I wan to check if there is any repeated number in n
end

where C{1}=['202743_at'
'203879_at'
'204297_at'
'204369_at'
'204484_at'
'206369_s_at'
'206370_at'
'207105_s_at'
'208442_s_at'
'210858_x_at'
'211230_s_at'
'211580_s_at'
'212672_at'
'212688_at'
'213070_at'
'215129_at'
'215394_at'
'217620_s_at'
];

Thank you in advance,
Wendy

Subject: random number generator

From: dpb

Date: 26 Feb, 2010 15:12:37

Message: 2 of 5

Wendy wrote:
...
> n = round(size(C{1},1)*(1/3)); %I wan to check if there is any
> repeated number in n
...
> where C{1}=['202743_at'
> '203879_at'
> '204297_at'
> '204369_at'
...
> '215394_at'
> '217620_s_at'
> ];
>
> Thank you in advance,

v=sscanf(char(C),'%d_at');
norepeat = isequal(length(unique(v)),size(C{1},1));

--

Subject: random number generator

From: Wendy

Date: 26 Feb, 2010 15:49:04

Message: 3 of 5

Hi,

Thank you for your help. My mistake in the testing code. A more simplified version should me,

n = round(rand(4,1)*3);
% This code generates a 4x1 array of numbers between 0 and 3. I round the numbers of integers. Is there a way that I can check for the repeated numbers in the final array (e.g, [0 1 1 3], and I want the code to return if there are repeated number).

Thank you very much,
Wendy
 
dpb <none@non.net> wrote in message <hm8ofi$cr6$1@news.eternal-september.org>...
> Wendy wrote:
> ...
> > n = round(size(C{1},1)*(1/3)); %I wan to check if there is any
> > repeated number in n
> ...
> > where C{1}=['202743_at'
> > '203879_at'
> > '204297_at'
> > '204369_at'
> ...
> > '215394_at'
> > '217620_s_at'
> > ];
> >
> > Thank you in advance,
>
> v=sscanf(char(C),'%d_at');
> norepeat = isequal(length(unique(v)),size(C{1},1));
>
> --
>

Subject: random number generator

From: Steven Lord

Date: 26 Feb, 2010 15:52:53

Message: 4 of 5


"Wendy " <wlq121@gmail.com> wrote in message
news:hm8qhg$31p$1@fred.mathworks.com...
> Hi,
>
> Thank you for your help. My mistake in the testing code. A more simplified
> version should me,
>
> n = round(rand(4,1)*3); % This code generates a 4x1 array of numbers
> between 0 and 3. I round the numbers of integers. Is there a way that I
> can check for the repeated numbers in the final array (e.g, [0 1 1 3], and
> I want the code to return if there are repeated number).

Are you looking for a permutation of 0:3? If so, use RANDPERM.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: random number generator

From: Xin

Date: 26 Feb, 2010 16:29:06

Message: 5 of 5

"Wendy " <wlq121@gmail.com> wrote in message <hm8qhg$31p$1@fred.mathworks.com>...
> Hi,
>
> Thank you for your help. My mistake in the testing code. A more simplified version should me,
>
> n = round(rand(4,1)*3);
> % This code generates a 4x1 array of numbers between 0 and 3. I round the numbers of integers. Is there a way that I can check for the repeated numbers in the final array (e.g, [0 1 1 3], and I want the code to return if there are repeated number).
>
> Thank you very much,
> Wendy
>

Hi Wendy, simply use the function unique:

>length(unique(V_tocheck))==length(V_tocheck))

Good luck!

Xin

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