How to vectorize random permutation of data
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Eric Fields
le 27 Nov 2016
Modifié(e) : Roger Stafford
le 28 Nov 2016
I need to randomly permute a set of data, and I need to do it 10,000 or more times, so I need to do it efficiently. Below is an example of how I'm doing it (with randomly generated data standing in for real data). I feel like there should be a way to vectorize the permutation process instead of the for-loop I'm using, but I can't think of how to do it. I need a method that works for any number of data points--i.e., below I am permuting two data points for each hypothetical subject, but I need to generalize to three, four, etc.
data = rand(24, 2);
for j = 1:24
perm_data(i, :) = data(i, randperm(2));
end
%Do some calculations on the permuted data here
0 commentaires
Réponse acceptée
Roger Stafford
le 28 Nov 2016
Modifié(e) : Roger Stafford
le 28 Nov 2016
You wish to randomly permute each of the rows of ‘data’. Then do this:
(Simplified)
[m,n] = size(data);
[~,p] = sort(rand(m,n),2);
perm_data = reshape(data(repmat((1-m:0).’,n,1)+p(:)*m),m,n);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!