random

3 visualizaciones (últimos 30 días)
Niki
Niki el 6 de Jun. de 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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 6 de Jun. de 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 comentarios
Niki
Niki el 6 de Jun. de 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 el 6 de Jun. de 2012
full number of permutation for matricies with size m x n : N = m^n

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by