필터 지우기
필터 지우기

generate unrepeated pairs from randi

조회 수: 2 (최근 30일)
mingcheng nie
mingcheng nie 2023년 9월 2일
편집: Bruno Luong 2023년 9월 2일
I have two variables A and B. They are independently generated from randi with different range. For example, A=randi([0,5],1,5); B=randi([-3,3],1,5);
Now I assume the element in A along with the corresponding element in B consist a pair C, where C is of size 1×5. However, I don't want any pair in C is repeated. If repeated, just generate B again until the pair is unrepeated. Is there any simple code can realize this?
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 9월 2일
Did you mean that C is 2x5?
Also, to confirm, if there is a repeated pair, you just want to regenerate B?

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

답변 (1개)

Bruno Luong
Bruno Luong 2023년 9월 2일
편집: Bruno Luong 2023년 9월 2일
a = 0:5;
b = -3:3;
n = 20; % 5 in your case
na = length(a);
nb = length(b);
[A,B] = ind2sub([na nb], randperm(na*nb, n));
A = a(A);
B = b(B);
C = [A(:) B(:)]
C = 20×2
5 -1 2 2 4 1 5 -3 3 -1 1 3 1 2 1 -2 1 1 4 -2

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by