random

3 vues (au cours des 30 derniers jours)
Niki
Niki le 6 Juin 2012
I have a Matrix as follows
x = [1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20];
I want to generate a matrix from it that will be for example 100 combination of each element but for example for the first iteration, from first colum only one of them select then from the second as well then from the third as well and untill the end
so I may have like this
x = [ 6 2 18 4 20
16 17 3 4 10
and so on

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 6 Juin 2012
x = [1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20];
EDIT after Mohammad's coment
[m,n] = size(x);
df = bsxfun(@plus,fullfact(m*ones(1,n)),(0:n-1)*m);
k = randperm(size(df,1));
out = x(df(k(1:min(100,numel(k))),:));
OR
[m,n] = size(x);
xc = mat2cell(x,m,ones(n,1));
cc = cell(1,n);
[cc{:}] = ndgrid(xc{:});
k = randperm(numel(cc{1}));
out = cell2mat(cellfun(@(x)x(k(1:min(100,numel(k)))'),cc,'un',0));
  3 commentaires
Niki
Niki le 6 Juin 2012
I want to have something lets say, number of permutation , can you also tell me what should I do with that?
for example I want to have only 100 generated row
more or less
Andrei Bobrov
Andrei Bobrov le 6 Juin 2012
full number of permutation for matricies with size m x n : N = m^n

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by