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에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!