필터 지우기
필터 지우기

generate two variable from distribution without same result

조회 수: 1 (최근 30일)
mingcheng nie
mingcheng nie 2023년 1월 11일
편집: Bruno Luong 2023년 1월 11일
Hi there, if we have two variables are generated from uniform distribution as : A=randi([0,5],1,6); B=randi([-4,4],1,6);
Now I want some simple steps that can make: A and B are both vector, and I define that C(i)=[A(i) B(i)], i=1:6; then I dont want any of rows of C are the same. How should I do for the randi to make this happen?
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 1월 11일
"I dont want any of rows of C are the same."
It is unclear to me what exactly it means. Please give an example.
Meanwhile, I have attempted something based on an assumption, let me know if it works or not.
A=randi([0,5],1,6)
A = 1×6
1 2 3 3 1 3
B=randi([-4,4],1,6)
B = 1×6
0 2 -1 0 3 0
while any(A==B)
B=randi([-4,4],1,6);
end
C=[A' B']
C = 6×2
1 -3 2 3 3 -3 3 2 1 0 3 -3
mingcheng nie
mingcheng nie 2023년 1월 11일
Sorry for my bad explanation, here I give an example:
A=[1 2 3 3 2 3];
B=[1 2 4 5 1 4];
we can observe that in the third and sixth postion, AB have the same values, I don't want this happen. But in the first and fifth postion, B's elements are same but A's elements are not, then this is fine. Only remove that both when A's and B's elements are the same and generate new value

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2023년 1월 11일
one of the ways is to prevent it from happening at all, by excluding the random A from the values available to be chosen from.
Achoices = 0:5;
numA = length(Achoices);
Bchoices = -4:4;
for row = 1 : 6
A = Achoices(randi(numA));
availableB = setdiff(Bchoices, A);
B = availableB(randi(numel(availableB)));
C(row,:) = [A, B];
end
Another way is to generate 6 entries freely without any conflict control, and then to remove rows that have duplicates, and then go back and generate as many more entries as you need. You would want a loop for this, each time generating (6 minus the number already generated), as it is possible for the new attempts to also happen to have conflicts.
Oh way... are you asking that the A and B entries of any one row are not the same, or is that acceptable but no two rows of C can be the same as other rows? If so then the loop generating some and removing duplicates and going back to generate any missing ones works fine.
Sometimes people improve the random efficiency by generating more entries than they need, expecting that some will probably clash, and hoping that enough non-clash will be left over after removing duplicates. You generally still need some looping if you do that, just not as many loop trips on average.
  댓글 수: 1
mingcheng nie
mingcheng nie 2023년 1월 11일
thank you for your answer, I give an example in the commet to make my question more clear

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2023년 1월 11일
편집: Bruno Luong 2023년 1월 11일
Warranty never repeated pairs
lA = 0; uA = 5; % lower upper bound of A
lB = -4; uB = 4; % and B
n = 50; % number of pair elements to be generated, 6 in your case
mA = (uA-lA)+1;
mB = (uB-lB)+1;
[iA,iB] = ind2sub([mA,mB], randperm(mA*mB,n));
A = lA-1+iA,
A = 1×50
4 3 4 1 3 0 2 1 1 3 3 0 5 3 0 0 0 4 3 0 4 1 4 2 5 0 1 5 5 1
B = lB-1+iB,
B = 1×50
4 2 0 -3 -4 -2 -3 2 -2 3 0 1 -2 4 2 -3 -4 -3 -2 0 3 1 -2 -1 -4 3 0 2 -3 -1

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by