random

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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-6-6
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 个评论
Niki
Niki 2012-6-6
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 2012-6-6
full number of permutation for matricies with size m x n : N = m^n

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by