To get a random number

Hi,
Am having a incremental column matrix like, e.g. for 1st iteration, it is 100*1(double), then for 2nd iteration iteration, it will be 100*2(double), etc.. Form this, for 1st iteration, i need to get a random no (single data only). from 1st column of matrix and for 2nd iteration, i have to get a random no. from 2nd column of matrix only.
Thank you in advance!!@!!

1 commentaire

Michael
Michael le 18 Jan 2012
Maybe I misunderstand but to me this question appears contradictory. How is a number random if it depends on your column as an input?

Réponses (1)

Wayne King
Wayne King le 18 Jan 2012

0 votes

You can use
x = randperm(100);
randomindex = x(1);
Then, use that index to choose an element from the appropriate column, say your data matrix is X
X(randomindex,1) %choose from first column
X(randomindex,2) %choose from 2nd column

7 commentaires

Muruganandham Subramanian
Muruganandham Subramanian le 18 Jan 2012
Hi wayne,
Am having negative integers also.
Wayne King
Wayne King le 18 Jan 2012
How can you have negative indices in your vectors? that's not possible.
Muruganandham Subramanian
Muruganandham Subramanian le 19 Jan 2012
Hi wayne,
to get the vectors(i.e. 100*1,100*2), i used unifrnd command in the range b/w -0.8 to 0.2 for 100 populations. From this, i have to pick a data randomly.
Walter Roberson
Walter Roberson le 19 Jan 2012
The range of your data matrix X does not affect the use of Wayne's suggested code.
However, to clarify his code a little:
rchoices = randperm(size(X,2));
randfirst = X(rchoices(1),1);
rchoices = randperm(size(X,2));
randsecond = X(rchoices(1),2);
Repeating the randperm() is required if you want it to be possible for the same row to be chosen for both columns. If the same row must _not_ be chosen for the columns, then
rchoices = randperm(size(X,2));
randfirst = X(rchoices(1),1);
randsecond = X(rchoices(2),2);
Muruganandham Subramanian
Muruganandham Subramanian le 20 Jan 2012
Hi walter,
It's not working.
am having both negative and non-negative integers of 100*1, In this i have to pick single data randomly.
Walter Roberson
Walter Roberson le 20 Jan 2012
Sorry, the references to size(X,2) should have been size(X,1)
rchoices = randperm(size(X,1));
randchoice = X(rchoices(1),IterationNumber);
Muruganandham Subramanian
Muruganandham Subramanian le 20 Jan 2012
It's working...
Thanks walter

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by