Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab-生成矩阵的随机坐标_Matlab_Random_Matrix_Coordinates_Permutation - Fatal编程技术网

Matlab-生成矩阵的随机坐标

Matlab-生成矩阵的随机坐标,matlab,random,matrix,coordinates,permutation,Matlab,Random,Matrix,Coordinates,Permutation,我需要在预定义大小的矩阵上创建一个随机、非重复坐标集的列表(大小为n) 有没有一种在Matlab中快速生成的方法 我最初的想法是创建一个大小为n的列表,其中包含大小为(宽度x长度)的排列,并将它们转换回行和列的值,但在我看来这似乎太多了 谢谢, Guy只要n小于矩阵中的元素数,这很简单: % A is the matrix to be sampled % N is the number of coordinate pairs you want numInMat = numel(A); % sa

我需要在预定义大小的矩阵上创建一个随机、非重复坐标集的列表(大小为n)

有没有一种在Matlab中快速生成的方法

我最初的想法是创建一个大小为n的列表,其中包含大小为(宽度x长度)的排列,并将它们转换回行和列的值,但在我看来这似乎太多了

谢谢,
Guy

只要
n
小于矩阵中的元素数,这很简单:

% A is the matrix to be sampled
% N is the number of coordinate pairs you want
numInMat = numel(A);

% sample from 1:N without replacement
ind = randperm(numInMat, N);

% convert ind to Row,Col pairs
[r, c] = ind2sub( size(A), ind )

只要
n
小于矩阵中的元素数,这很简单:

% A is the matrix to be sampled
% N is the number of coordinate pairs you want
numInMat = numel(A);

% sample from 1:N without replacement
ind = randperm(numInMat, N);

% convert ind to Row,Col pairs
[r, c] = ind2sub( size(A), ind )

您可以使用
randperm
生成线性索引,并在需要时使用
ind2sub
将其转换为[row,col]

x = rand(7,9);
n = 20;
ndx = randperm(numel(x), n);
[row,col] = ind2sub(size(x), ndx);

您可以使用
randperm
生成线性索引,并在需要时使用
ind2sub
将其转换为[row,col]

x = rand(7,9);
n = 20;
ndx = randperm(numel(x), n);
[row,col] = ind2sub(size(x), ndx);

您的想法很好,尽管您甚至不必将线性索引转换回行和列索引,但您可以直接将线性索引转换为二维数组

idx = randperm(prod(size(data)))
数据就是你的矩阵。这将生成1到
prod(大小(数据))
之间的随机整数向量,即每个元素一个索引

e、 g


您的想法很好,尽管您甚至不必将线性索引转换回行和列索引,但您可以直接将线性索引转换为二维数组

idx = randperm(prod(size(data)))
数据就是你的矩阵。这将生成1到
prod(大小(数据))
之间的随机整数向量,即每个元素一个索引

e、 g


如果
n
大于矩阵中的元素数,会发生什么情况?那么重复是否可以接受?我已经将项目上传到git:如果
n
大于矩阵中的元素数会发生什么?那么重复是否可以接受?我已经将项目上传到git:
,尽管您甚至不必将线性索引转换回行和列索引
,但OP特别要求一种生成坐标的方法,而不是线性的indices@slayton我想你是对的,我认为OP只是在寻找一个索引方案,而不是试图为其他目的生成坐标。我应该删除这个答案吗?嗯,这不是一个糟糕的答案,我会保留它
,尽管你甚至不需要将线性索引转换回行和列索引
,但是OP特别要求一种生成坐标的方法,而不是线性的indices@slayton我想你是对的,我认为OP只是在寻找一个索引方案,而不是试图为其他目的生成坐标。我应该删除这个答案吗?嗯,这个答案不错,我会保留它的