【问题标题】:MATLAB - Best way of creating 2d array of unique pairs?MATLAB - 创建唯一对的二维数组的最佳方法?
【发布时间】:2016-02-27 02:40:57
【问题描述】:

在 matlab 中创建 10x2 矩阵的最佳方法是什么,其中每个元素都是 1-5 之间的随机整数,因此该数组中只有唯一的元素对?我知道 randperm 可以给我随机的唯一数字,但我不确定是否可以使用 randperm 给唯一的对?我能想到的唯一其他方法是使用:

randi([1 5], 10, 2);

在使用 if 语句检查所有对是否唯一的循环中。 我想要的数据示例如下:

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

注意:元素的顺序无关紧要,例如,4, 5 和 5, 4 都有效。

【问题讨论】:

  • 虽然这是一个奇怪的问题。如果您随机选择 1 到 5 之间的整数,则无法保证您将获得 10 个唯一的值对。保证它的唯一方法是您只使用1-5 作为输入整数。
  • 是否允许[1,2;2,1]
  • @Daniel 是的,他说元素的顺序无关紧要。
  • @excaza:但是如果订单无关紧要,我希望它是重复的并且不允许。

标签: arrays matlab random


【解决方案1】:

首先将所有可能的对生成为矩阵的行,然后使用randperm 生成行索引的随机子集:

N = 5;                                    %// alphabet size
M = 2;                                    %// number of columns
P = 10;                                   %// desired number of rows
allPairs = dec2base(0:N^M-1, N)-'0'+1;    %// generate all possible rows
ind = randperm(size(allPairs,1));         %// indices for random permutation of rows
ind = ind(1:P);                           %// pick P unique indices
result = allPairs(ind,:);                 %// use those indices to select rows

示例结果:

result =
     3     2
     1     4
     3     5
     4     1
     1     3
     1     2
     2     4
     3     4
     5     5
     1     5

【讨论】:

  • 根据这些功能再发一个,希望没问题:)
【解决方案2】:

根据 excaza 的评论,我发现这符合我的需要:

n = randperm(5);
k = 2;
data = nchoosek(n, k);

这给出了一些示例输出:

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

【讨论】:

  • 但这只是为每一对提供了一个可能的排序。例如,如果1 2 出现在输出中,则可以确保2 1 永远不会出现。此外,您永远不会得到具有两个相等值的行。特别是,您的答案中给出的示例输出将永远不会出现。要么你的问题没有很好地表述,要么这个答案是错误的。我认为这是错误的答案,很抱歉,我投反对票
【解决方案3】:

这是另一种使用randpermdec2base 的方法,没有生成所有可能的行的内存开销(引用Luis's solution)-

%// Inputs
start = 1
stop = 5
Nr  = 10                    %// Number of rows needed
Nc = 2                      %// Number of cols needed

intv = stop - start + 1;                    %// Interval/range of numbers
rand_ID = randperm(power(intv,Nc)-1,Nr);    %// Unique IDs
out = dec2base(rand_ID,intv) - '0'+ start   %// 2D array of unique numbers

示例运行 -

案例#1(与问题中列出的参数相同):

start =
     1
stop =
     5
Nr =
    10
Nc =
     2
out =
     1     3
     2     1
     5     3
     5     4
     5     5
     3     4
     2     3
     2     5
     3     3
     1     4

案例#2(不同的参数):

start =
        1025
stop =
        1033
Nr =
    10
Nc =
     5
out =
        1030        1029        1033        1028        1029
        1033        1029        1026        1025        1025
        1028        1026        1031        1028        1030
        1028        1031        1027        1028        1025
        1033        1032        1031        1029        1032
        1033        1029        1030        1027        1028
        1031        1025        1032        1027        1025
        1033        1033        1025        1028        1029
        1031        1033        1025        1033        1029
        1028        1025        1027        1028        1032

【讨论】:

  • 请注意,dec2base 仅适用于 2 dec2base。 (如何缩短 cmets 中的链接?)
  • @FirefoxMetzger [ ` some_text ` ](link) 没有空格。是的,我猜这是一个限制,感谢您提供的信息!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2023-04-03
  • 2015-08-31
相关资源
最近更新 更多