How can I create a random array like the example C?

A=[1 1 2 2 7 7 9 9]
B=[3 5 6 8 10 11] : randomly choose all items from B and input between 2 same number of A
for example : C=[1 8 6 1 2 5 3 2 7 11 7 9 10 9]

4 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 4 月 25 日
編集済み: KALYAN ACHARJYA 2019 年 4 月 25 日
Hello Hang, this is not random array, its have certain pattern. I have tried in your v1,v2 question, still not getting the exact results as per requirement.
Hang Vu
Hang Vu 2019 年 4 月 25 日
Thank you for the response! I am coding with genetic algorithm, so I am trying to make the chromosome by that way. I don't know if it is possiple. Because for 1 set of sequence, I may have 3 or 4 of "v". So I decided to combine all v into C.
Jan
Jan 2019 年 4 月 25 日
Please mention the details: Does the result need to have at least one value of B between the equal elements of A? Does the order of elements of B matter? Does A have an even number of elements in every case so the new elements can be inserted at the indices 2, 4, 6, ...? Providing 1 example does not define the problem completely.
Hang Vu
Hang Vu 2019 年 4 月 26 日
Thank you Jan! yes at least one value of B between 2 same number of A, and the B'order is no matter. And A is has 2 same number for each elements so it's even number of elements.

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

 採用された回答

Stephen23
Stephen23 2019 年 4 月 26 日
編集済み: Stephen23 2019 年 4 月 26 日

1 投票

A = [1,1,2,2,7,7,9,9];
B = [3,5,6,8,10,11];
Na = numel(A);
Nb = numel(B);
Xb = randperm(Nb) % to shuffle the order of B.
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
C = [T{:}]
Giving (for example):
Xb =
4 6 3 2 1 5
Xa =
1 2 2 1
C =
1 8 1 2 11 6 2 7 5 3 7 9 10 9

6 件のコメント

Hang Vu
Hang Vu 2019 年 4 月 26 日
it is so cool! Thank you so much.
But I quite don't understand the Xa = diff([0,sort(Xa),Nb]).
I just tested some cases like if A has more elements and B has less elements by changing D then it doesn't work for all cases. I am sorry by misunderstanding but I just realized we can allow 0 element between A, if don't have enough B to fill in
D = [170, 80, -30, 50, -50, 30, 20, -60, 100, 60 -20];
Iplus=find(D>0);
A=repelem(Iplus,2);
Iminus=find(D<0);
Na = numel(A);
Nb = numel(Iminus);
Xb = randperm(Nb) % to shuffle the order of B.
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
C = [T{:}]
Stephen23
Stephen23 2019 年 4 月 26 日
編集済み: Stephen23 2019 年 4 月 26 日
"But I quite don't understand the Xa = diff([0,sort(Xa),Nb])."
The concatenation operation [] generates a vector of indices of the ends of blocks of elements, where each block has a random length. The diff then gets those block lengths, which can be used with mat2cell. Because randperm's outputs do not repeat, this guarantees that each block contains at least one element (exactly as you requested).
"I am sorry by misunderstanding but I just realized we can allow 0 element between A"
You wrote that "at least one value of B between 2 same number of A", so that is exactly what my answer does. If you want to change the specification then clearly the code will need to be changed, e.g.:
A = [1,1,2,2,7,7,9,9];
B = [3,5];
Na = numel(A);
Nb = numel(B);
Xb = randperm(Nb) % to shuffle the order of B.
if Nb<=Na/2 % "if don't have enough B to fill in"
Xa = zeros(1,Na/2);
Xa(randperm(Na/2,Nb)) = 1; % the number of elements of B to pick.
else
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
end
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
C = [T{:}]
Giving (for example):
Xb =
1 2
Xa =
0 1 1 0
C =
1 1 2 3 2 7 5 7 9 9
Hang Vu
Hang Vu 2019 年 4 月 26 日
Thank you for response! I tried to change D, but seem it doesn't work well
D = [170, 80, 30, 80, 50, 30, 20, -60, 100, -60 -20];
Iplus=find(D>0);
A=repelem(Iplus,2);
Iminus=find(D<0);
Na = numel(A);
Nb = numel(Iminus);
Xb = randperm(Nb) % to shuffle the order of B.
if Nb<=Na/2 % "if don't have enough B to fill in"
Xa = zeros(1,Na/2);
Xa(randperm(Na/2,Nb)) = 1; % the number of elements of B to pick.
else
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
end
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
C = [T{:}]
but the result:
Xb =
1 2 3
Index exceeds the number of array elements (2).
Error in test26_4 (line 18)
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
Stephen23
Stephen23 2019 年 4 月 26 日
編集済み: Stephen23 2019 年 4 月 26 日
"I tried to change D, but seem it doesn't work well..:"
I have no idea what D is or how it relates to your question or my answer.
You wrote your question specifying two input vectors A and B. If you show an example with those two vectors (and explain what you expect and what you get) then I can help you further.
Hang Vu
Hang Vu 2019 年 4 月 26 日
編集済み: Hang Vu 2019 年 4 月 29 日
I just realized I didn't change B to Iminus. Now it works well. Thank you so much!^^
Hang Vu
Hang Vu 2019 年 4 月 29 日
Could you please help me more on this! for all cases, can be either pick 1 1 or 2 numbers!
Xb =
1 2
Xa =
0 1 1 0 => 0 0 2 0
C =
1 1 2 3 2 7 5 7 9 9 => 1 1 2 2 7 3 5 7 9 9
if Nb<=Na/2 % "if don't have enough B to fill in"
Xa = zeros(1,Na/2);
Xa(randperm(Na/2,Nb)) = 1; % the number of elements of B to pick.
else
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
end
and it's possible to have 0 for all cases, not only Nb<=Na

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 4 月 25 日

コメント済み:

2019 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by