Could anyone help me how to solve the issue in the code as attached

1 次查看(过去 30 天)
The following code executes and display the result with 1x1 double, 1x2 double and 1x3 double.
A= arrayfun( @my_rand, repelem((1:3).',5), 'UniformOutput', false);
function seq = my_rand(N)
if N == 1
seq = 1;
return;
end
max = randi(N);
s1 = randperm(max);
s2 = randi(max,1,N-max);
seq = [s1 s2];
seq = seq(randperm(N));
seq = sort(seq);
end
Could anyone help me how to execute the result with 10x1 double, 10x2 double and 10x3 double.
  1 个评论
Walter Roberson
Walter Roberson 2021-7-15
max is at most N and s1 is that length. s2 is then constructed to be the remaining length so that between the two s1 and s2 total N. You put s1 and s2 together to get seq, which will have total length N. You use randperm(N) to randomize the order of seq. So far so good.
But then you sort seq. I do not understand why you do that? You just carefully randomized the order. If you needed sorted output why did you bother to randomize the order first? The sort would be the same whether you randomized the order or not.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-7-15
The following code makes the assumption that in each group of 10, that the breakpoint "Max" is to be chosen independently. The code would be different (no loop needed) if the breakpoint for each group is to be the same.
m = 10; %rows
A = arrayfun( @(n)my_rand(n,m), repelem((1:3).',5), 'UniformOutput', false);
celldisp(A)
A{1} = 1 1 1 1 1 1 1 1 1 1 A{2} = 1 1 1 1 1 1 1 1 1 1 A{3} = 1 1 1 1 1 1 1 1 1 1 A{4} = 1 1 1 1 1 1 1 1 1 1 A{5} = 1 1 1 1 1 1 1 1 1 1 A{6} = 1 2 1 2 1 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 A{7} = 1 1 1 1 1 1 1 2 1 2 1 1 1 2 1 1 1 1 1 2 A{8} = 1 1 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 A{9} = 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 A{10} = 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 A{11} = 1 2 3 1 1 2 1 1 1 1 2 2 1 2 2 1 2 2 1 2 3 1 1 1 1 2 2 1 1 1 A{12} = 1 1 2 1 2 2 1 2 2 1 2 3 1 2 3 1 2 3 1 2 3 1 1 1 1 2 3 1 2 3 A{13} = 1 2 2 1 1 1 1 1 2 1 1 2 1 2 2 1 1 2 1 1 1 1 2 3 1 1 2 1 1 1 A{14} = 1 2 3 1 2 3 1 2 3 1 2 2 1 2 2 1 1 1 1 1 1 1 2 2 1 2 3 1 2 3 A{15} = 1 1 2 1 2 3 1 1 1 1 2 3 1 2 3 1 1 2 1 1 2 1 1 1 1 1 1 1 2 3
function seq = my_rand(N,M)
if N == 1
seq = ones(M,1);
return;
end
seq = zeros(M,N);
for K = 1 : M
Max = randi(N);
s1 = randperm(Max);
s2 = randi(Max,1,N-Max);
Seq = sort([s1 s2]);
seq(K,:) = Seq;
end
end

更多回答(1 个)

Simon Chan
Simon Chan 2021-7-15
If I understand correctly, try this:
A = arrayfun( @my_rand, repelem((1:3).',5), 'UniformOutput', false);
B = cellfun(@(x) repmat(x,10,1),A,'UniformOutput',false)
  1 个评论
jaah navi
jaah navi 2021-7-15
Yes, you understanding is correct. But if i need to have different combinations inside each array i.e, for 10x3 I can see all the 10 rows are having the same numbers repeated as
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
1 2 2
So could you please help me to have different combinations as
1 2 2
1 1 2
1 2 2
1 2 2
1 2 3
1 2 2
1 2 2
1 1 2
1 1 1
1 2 3

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by