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

1 view (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Walter Roberson
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)
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

More Answers (1)

Simon Chan
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)
  1 Comment
jaah navi
jaah navi on 15 Jul 2021
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

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!