Could anyone help me how to solve the issue in the code as attached
1 view (last 30 days)
Show older comments
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 Comment
Walter Roberson
on 15 Jul 2021
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.
Accepted Answer
Walter Roberson
on 15 Jul 2021
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
More Answers (1)
Simon Chan
on 15 Jul 2021
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)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!