Matlab: Get a list of ordered pairs

Let me explain with an example: if I give “4” as input, I need pairs (1,2), (1,3), (1,4), (2,3), (2, 4), (3.4) in such a matrix:

1 2
1 3
1 4
2 3
2 4
3 4

Why do I need it? I will create a random number from 1 to (n * (n-1) / 2), and I want a matching pair. I decided that I would first create a list of pairs and then use a random number as an index. If there is a direct solution to this, then much better!

Edit: Actually, I don’t need all the pairs, I need t random ordered pairs that do not repeat, and the first value is <the second value. There are 5,000 listings with n items. Thus, the total number of pairs is tot = 5000 * n * (n-1) / 2. If I generate t random integers from 1 to tot using randperm, can I pair it? I can get the list number by dividing it by 5000. And if I can with 5000, I will get a paired index. I tried to convert this front index into a pair. Sorry for not explaining all this before. I hope I get it.

+4
source share
3 answers

In the first part of your question: you want combinations of elements to be n=4accepted k=2at a time. There is a function for this, namely nchoosek:

n = 4; %// number of elements
k = 2; %// how many to pick in each group
result = nchoosek(1:n, k);

: :

n = 4;
x = randi(n); %// uniformly distributed on {1,2,...,n}
y = randi(n-1);
y = y + (y>=x); %// uniformly distributed on {1,2,...,x-1,x+1,...,n}
pair = sort([x y]);
+2

, N, ( ):

  • x 1 n-1
  • y 1 n-2

- n-1

y < x, y, y + 1


, , , Matlab :

sort(randsample(4,2))
+2

, . x, y, x, (x | y)! , , . i, (x | y) :

x \ y   1   2   3   4
1           #1  #2  #3
2               #4  #5
3                   #6 

. (1 | 2) ↔ # 1 .. , , . , . "" , :

x \ y   1       2       3       4
1       (#1)    #2      #3      #4
2       (#5)    (#6)    #7      #8
3       (#9)    (#10)   (#11)   #12

And skip those in parentheses when they are generated. Now mapping a function from index ito is (x | y)much simpler:

i = (x-1) * N + y

x = floor(i/N) + 1
y = i - (x-1)*N + 1

The disadvantage is that we can never be absolutely sure that we have enough samples on the desired side of the diagonal, so we must be prepared for this. However, the probability of this is quite small.

N = 6 % maximal x and y
S = 10 % number of samples to draw

ind = [];
% This loop will most likely run only once, 
% but we can not be completely sure
while size(ind, 1) < S
    % (*)
    % In order to test uniformity of distribution, we can 
    % generate some repeating data instead of the next line:
    % i = ceil(N*N .* rand(3*S, 1)); 

    % We generate 3*S > S samples, because at least 50% will be removed
    i = randsample(N*N, min(N*N, 3*S)); 

    x = floor(i/N) + 1;
    y = i - x*N + N + 1;

    remove = (x >=  y);
    x(remove, :) = [];
    y(remove, :) = [];

    ind = [x y];
end    
ind = ind(1:S, :);

% verify:
test = accumarray(ind, ones(S,1))

This may cause

ind =
     2     6
     3     4
     1     4
     3     6
     5     6
     2     3
     1     6
     3     5
     4     6
     1     5   

test =
     0     0     0     1     1     1
     0     0     1     0     0     1
     0     0     0     1     1     1
     0     0     0     0     0     1
     0     0     0     0     0     1

And a uniformity test for S = 10000:

test =
     0   671   716   686   691   664
     0     0   644   650   667   664
     0     0     0   672   649   654
     0     0     0     0   667   655
     0     0     0     0     0   650
0
source

All Articles