Passing through each element in a matrix just once and randomly
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Carlos
el 25 de Abr. de 2016
Comentada: John BG
el 27 de Abr. de 2016
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
0 comentarios
Respuesta aceptada
Roger Stafford
el 25 de Abr. de 2016
B = A(randperm(prod(size(A))));
The vector B will have each value of A once and only once in random order.
2 comentarios
Más respuestas (2)
Image Analyst
el 26 de Abr. de 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);
0 comentarios
John BG
el 26 de Abr. de 2016
Editada: John BG
el 26 de Abr. de 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 comentarios
Roger Stafford
el 26 de Abr. de 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
el 27 de Abr. de 2016
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!