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
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
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)
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
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)
参考
カテゴリ
Help Center および File Exchange で Stability Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!