My command is B = arrayfun(@(N) randi(2,1,N), repelem((1:3).',5), 'uniform', 0)
when i run it generates B= 15×1 cell array
{[ 2]}
{[ 1]}
{[ 3]}
{[ 3]}
{[ 2]}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double}
but the values inside the array are
1
1
2
1
2
[2,1]
[2,1]
[1,2]
[2,2]
[1,1]
[2,2,1]
[1,2,1]
[2,2,2]
[1,1,1]
[2,1,2]
Actually i need to get
1
1
3
1
1
for first 5 rows the value should be 1 as it is 1x1 double
[1,2]
[2,1]
[3,2]
[3,2]
[2,1]
for the next 5 rows the values should be 1,2 and not more than 2, and should not have 2 by leaving 1
[1,2,1]
[3,1,3]
[2,3,3]
[1,2,2]
[3,3,1]
for the next 5 rows the values should not have 3 by leaving 2 and should not have 2 by leaving 1.
Could anyone please help me with this.

 採用された回答

Mohammad Sami
Mohammad Sami 2021 年 6 月 24 日

0 投票

I am bit confused what you mean by should not have 3 by leaving 2 and should not have 2 by leaving 1.
I assume you just want the numbers in order 1,2,3.
B = arrayfun(@(N) colon(1,N), repelem((1:3).',5), 'UniformOutput', false)
B = 15×1 cell array
{[ 1]} {[ 1]} {[ 1]} {[ 1]} {[ 1]} {[ 1 2]} {[ 1 2]} {[ 1 2]} {[ 1 2]} {[ 1 2]} {[1 2 3]} {[1 2 3]} {[1 2 3]} {[1 2 3]} {[1 2 3]}

11 件のコメント

jaah navi
jaah navi 2021 年 6 月 24 日
for first five rows the value should be one as mentioned , for next rows it can be of any possibilities of 1 and 2 i.e, 11, 12, 21 and should not be the values of 22 by leaving 1 as size is 1x2
For 11-15 rows it can be any possibilities of 1,2 and 3 and should not be the values of 333, 223 by leaving 1. as size is 1x3
if my explanation is not clear please reply me.
Mohammad Sami
Mohammad Sami 2021 年 6 月 24 日
so essentially you want random permutation of [1,2,3] ?
B = arrayfun(@(N) randperm(N), repelem((1:3).',5), 'UniformOutput', false)
B = 15×1 cell array
{[ 1]} {[ 1]} {[ 1]} {[ 1]} {[ 1]} {[ 1 2]} {[ 2 1]} {[ 2 1]} {[ 1 2]} {[ 1 2]} {[3 2 1]} {[1 2 3]} {[3 1 2]} {[2 3 1]} {[1 3 2]}
jaah navi
jaah navi 2021 年 6 月 24 日
for 6-10 rows now its not more than 2 what i actually need, but in addition i also need to have 11 in addition to 12, 21.
in the similar manner for 11-15 rows i can have any of the following values such as 112, 122, 212, 121, instead of all the rows containing 123.
could you please help me on this.
Mohammad Sami
Mohammad Sami 2021 年 6 月 24 日
I think you will need to write your own random function to generate these, since the built in functions don't have the behaviour you need.
function seq = my_rand(N)
if N == 1
seq = 1;
return;
end
pass = false;
while ~pass
seq = randi(N,1,N);
pass = all(ismember(colon(1,max(seq)),seq));
end
end
B = arrayfun(@my_rand, repelem((1:3).',5), 'UniformOutput', false)
jaah navi
jaah navi 2021 年 6 月 24 日
thanks for your help.
please let me know how to sort the values in each row of the cell array.
1
1
1
1
1
[2,1] - [1,2]
[1,2] - [1,2]
[1,1]
[1,2]
[1,2]
[3,1,2] - [1,2,3]
[3,1,2] - [1,2,3]
[1,3,2] - [1,2,3]
[2,1,1] - [1,1,2]
[3,1,2] - [1,2,3]
Mohammad Sami
Mohammad Sami 2021 年 6 月 24 日
You can just edit the my_rand function and add sort at the end.
seq = sort(seq);
jaah navi
jaah navi 2021 年 6 月 24 日
Thanks. I have done sorting the values in each rows using the command line cellfun(@sort,B,'UniformOutput',false) which works similar to seq = sort(seq);
But I want to sort the rows of the cell array. Suppose my cell array containing rows as
[1,2]
[1,2]
[1,1]
[1,2]
[1,1]
I want those rows to be sorted in the following manner
[1,1]
[1,1]
[1,2]
[1,2]
[1,2].
Could you please help me on this.
jaah navi
jaah navi 2021 年 7 月 5 日
I would like to check if i run the code its taking long time to execute more than a hour.
function seq = my_rand(N)
if N == 1
seq = 1;
return;
end
pass = false;
while ~pass
seq = randi(N,1,N);
pass = all(ismember(colon(1,max(seq)),seq));
end
end
B = arrayfun(@my_rand, repelem((1:24).',2000), 'UniformOutput', false)
So, please let me know is there any way to execute it faster.
Mohammad Sami
Mohammad Sami 2021 年 7 月 5 日
How about we change the function to a combination of randperm, randi
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));
end
jaah navi
jaah navi 2021 年 7 月 5 日
Thanks it works. Also, I would like to check the following issue.
Based on the above command
A = arrayfun( @my_rand, repelem((1:5).',300)', '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));
end
B = arrayfun( @my_rand, repelem((1:5).',300)', '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));
end
Here, I am having two array A and B
when I run the code i can get the A and B
But the difference between A and B is very large. As 900 array are created for A and B is there any way to create the maximum number same values in both A and B. For example 800 array should be generated as equal and the rest 100 can be different. could you please help me on it.
Mohammad Sami
Mohammad Sami 2021 年 7 月 6 日
You can simply generate A or another variable. Then you can use randperm to sample A to get B. For example if you have an array of 20 values and you want to choose 10, you can do like this.
if true
A = 1:2:40;
B = A(randperm(20,10));
end

サインインしてコメントする。

その他の回答 (1 件)

jaah navi
jaah navi 2021 年 7 月 15 日

0 投票

The following code executes as per required.
A = arrayfun( @my_rand, repelem((1:24).',500), '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
But in addition I want the above code to execute
10x1 double, 10x2 double, 10x3 double,......, 10x24 double instead 1x1 double, 1x2 double, 1x3 double,......, 1x24 double.
Could you please help me on this.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2021 年 6 月 24 日

回答済み:

2021 年 7 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by