How to generate random indicies?

I would like to generate k different random indices from 1 to N. One solution is randperm(N,k), however I do not need to shuffle the indicies. Are there any other solution directly generating idicies in order?

Réponses (2)

Image Analyst
Image Analyst le 31 Jan 2015
Modifié(e) : Image Analyst le 31 Jan 2015

2 votes

Yes, but if they're not "shuffled" then they're not random, are they? So you can get indices in ascending numerical order from 1 to N this way:
indices = 1 : N;
If you want what you said, but just sorted , then sort them:
pseudoRandomIndices = sort(randperm(N, k), 'ascend');

4 commentaires

Mr M.
Mr M. le 31 Jan 2015
k is less than N, so for example randperm(10,3) gives back 9,2,5 but I want them in order: 2,5,9
You have to use the entire ‘pseudoRandomIndices’ assignment. It returns them in ascending order:
N = 10;
k = 3;
pseudoRandomIndices = sort(randperm(N, k), 'ascend')
returns:
pseudoRandomIndices =
2 6 8
Mr M.
Mr M. le 1 Fév 2015
I can sort! This is not the question!
The algorithm is unnecessarily complicated (computational cost is unnecessarily big for large N)
Image Analyst
Image Analyst le 1 Fév 2015
We're not sure which algorithm you're thinking of, sort or randperms, but feel free to rewrite either one with a less complicated version of them. We're not going to do that for you. Good luck.

Connectez-vous pour commenter.

Matt J
Matt J le 1 Fév 2015
Modifié(e) : Matt J le 1 Fév 2015

0 votes

This avoids sorting,
idx=false(1,N);
vals=1:N;
idx(randperm(N, k))=1;
result=vals(idx);

Catégories

Question posée :

le 31 Jan 2015

Commenté :

le 1 Fév 2015

Community Treasure Hunt

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

Start Hunting!

Translated by