Clear Filters
Clear Filters

Passing through each element in a matrix just once and randomly

3 views (last 30 days)
Hello everyone, I have a very big nxn matrix which I want to visit element-per-element randomly, without repetition, i.e. first the A(4,100) term, then the A(53,20) term, and so on. I'm not saying I want the value of each element to be random, I want the order in which each element is visited to be random, and if possible to go through each element just once, due to the vastness of the matrix (2500x2500). I'm thinking the way to go is in the posible permutations between two 1-to-2500 groups of elements. But I'm not completely sure about that approach. Any help will be highly appreciated, Carlos

Accepted Answer

Roger Stafford
Roger Stafford on 25 Apr 2016
B = A(randperm(prod(size(A))));
The vector B will have each value of A once and only once in random order.

More Answers (2)

Image Analyst
Image Analyst on 26 Apr 2016
Carlos:
If you want "I want the order in which each element is visited" and "the way to go" then I would get the indexes of that route, path, sequence of indexes, or whatever you want to call it, like this:
linearIndexes = randperm(numel(A)); % All indexes scrambled up.
This gives the indexes to visit. If you want the values at those indexes, then you can use Roger's method, which is essentially
randomValues = A(linearIndexes);

John BG
John BG on 26 Apr 2016
Edited: John BG on 26 Apr 2016
With Stafford's answer, there is a chance you do not get certain combinations and repeat some others, if it's an image, certain pixels, while you may repeat other pixels.
The exhaustive way is to randomly choose one by one, but while avoiding repetition, go through all possible combinations.
for that purpose:
P=combinations(1:2500,1:2500);
L=randperm(length(P)); % L is 6250000
for k=L
A(k,:)
% process here
end
If you find this answer of any help solving your question,
please click on the thumbs-up vote link, thanks in advance
John
  8 Comments
Roger Stafford
Roger Stafford on 26 Apr 2016
To John: You speak of the "small possibility of getting the same shuffle". However, in so doing you seriously misinterpret Carlos' request, in my opinion. He says nothing about a repeated visit with a repeated "shuffle". He wants a single random ordering of the elements in his vector, and if he happens to by chance get the same order as he already had, that is presumably all right - it is one of the possible permutations. (Of course, with 6,250,000-factorial possible permutations the chance of that is mighty small.) Note carefully what the word 'permutation' means, John. It means that each quantity in the original list occurs again in the permuted ordering of that list just once and only once - no repetitions, no omissions.
In any case, this is what Matlab's 'randperm' function does, and I believe it does a very satisfactory job. I have observed the logic behind the 'randperm' operation. For "randperm(N)" it computes N successive random values, each ranging from zero to one using the 'rand' function, and then it sorts these values. It uses the permutation that is involved in that sorting as its output. If you assume that the 'rand' function is doing its job properly, and that 'sort' doesn't lose track of some of its values, this means that the result is a valid permutation and each of the N-factorial possible permutations is equally likely. I believe that is all Carlos is asking. If you doubt that, I suggest you direct your question to him directly.
John BG
John BG on 27 Apr 2016
agreed, we here locking horns and the originator saying nothing.
All the best
John

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!