Passing through each element in a matrix just once and randomly

4 次查看(过去 30 天)
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

采纳的回答

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

更多回答(2 个)

Image Analyst
Image Analyst 2016-4-26
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 2016-4-26
编辑:John BG 2016-4-26
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 个评论
Roger Stafford
Roger Stafford 2016-4-26
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 2016-4-27
agreed, we here locking horns and the originator saying nothing.
All the best
John

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by