我想从矩阵中提取(方形)子矩阵,随机调整大小介于最小值和最大值之间。
我还想保留所选的行和列索引。
是否有用于此目的的内置功能?我将不胜感激任何通用算法来实现所需的结果。
仅仅是一个不考虑方阵约束的例子:
输入:
| 1 2 3 4 5 6 7 8 <- column indices
--+----------------
1 | 4 3 1 4 0 1 0 1
2 | 2 0 1 5 6 3 2 0
3 | 5 5 0 6 7 8 9 0
4 | 2 3 5 6 7 9 0 1
^
Row indices
输出样本:
| 1 4 5 7 8 <- column indices
--+----------
1 | 4 4 0 0 1
2 | 2 5 6 2 0
4 | 2 6 7 0 1
^
Row indices
编辑:我想要一个像这样的功能
matrixsample(numberOfSamples, minSize, maxSize, satisfyingFunction)
样本的大小应在minSize
和maxSize
之间变化。
如果numberOfSamples = 10, minSize = 2 and maxSize = 6
那么输出应该是随机选择的行和列,如:
sampleMatrix1 2x2 sampleMatrix2 3x3 sampleMatrix3 5x5 ... sampleMatrix7 6x6 ... sampleMatrx10 4x4
satisfyingFunction
可以测试矩阵的任何属性;喜欢非单数,排名&gt; x
等等。
答案 0 :(得分:2)
在MATLAB中,您会发现randi([minVal, maxVal])
函数对于选择随机行/列非常有用。
要获得随机大小的子矩阵,请使用minVal
获取maxVal
和M = randi([0,9],4,8); % 4x8 matrix of random 1 digit integers, your matrix here!
nRows = randi([2, 4]); % The number of rows to extract, random between 2 and 4
nCols = randi([5, 7]); % The number of columns to extract, random between 5 and 7
idxRows = sort(randperm(size(M,1), nRows)); % Random indices of rows
idxCols = sort(randperm(size(M,2), nCols)); % Random indices of columns
output = M(idxRows, idxCols); % Select the sub-matrix
之间的随机整数。
nRows
<小时/>
如果您想使子矩阵平方,则只需使用nCols
和M = [4 3 1 4 0 1 0 1; 2 0 1 5 6 3 2 0; 5 5 0 6 7 8 9 0; 2 3 5 6 7 9 0 1];
idxRows = [1 2 4]; idxCols = [1 4 5 7 8];
output = M(idxRows, idxCols)
% >> 4 4 0 0 1
% 2 5 6 2 0
% 2 6 7 0 1
的相同值。
<小时/>
显示此方法适用于您的示例输入/输出值:
function output = getsubmatrix(M, nRows, nCols)
% Get a submatrix of random rows/columns
idxRows = sort(randperm(size(M,1), nRows));
idxCols = sort(randperm(size(M,2), nCols));
submatrix = M(idxRows, idxCols);
output = {submatrix, idxRows, idxCols};
end
您可以将上面的内容打包成一个简短的函数,它返回行索引和列索引,以及子矩阵。
function samples = matrixsample(matrix, numberOfSamples, minSize, maxSize, satisfyingFunction)
% output SAMPLES which contains all sample matrices, with row & column indices w.r.t MATRIX
samples = cell(numberOfSamples,1);
% maximum iterations trying to satisfy SATISFYINGCONDITION
maxiters = 100;
for ii = 1:numberOfSamples
iters = 0; submatrixfound = false; % reset loop exiting conditions
nRows = randi([minSize, maxSize]); % get random submatrix size
nCols = nRows; % Square matrix
while iters < maxiters && submatrixfound == false
% Get random submatrix, and the indices
submatrix = getsubmatrix(matrix, nRows,nCols);
% satisfyingFunction MUST RETURN BOOLEAN
if satisfyingFunction(submatrix{1})
samples{ii} = submatrix; % If satisfied, assign to output
submatrixfound = true; % ... and move on!
end
iters = iters + 1;
end
end
end
然后你可以在你描述的一些采样函数中使用它:
s = matrixsample(magic(10), 5, 2, 8, @(M)max(M(:)) < 90)
测试示例:
<?php
include("page2.php");
class account_Creation
.
.
$logging= new Logging; //The brackets should be removed
.
.
答案 1 :(得分:0)
如果提供table
,您将按randsample
:
minRowVal = 3;
maxRowVal = 4;
minColVal = 2;
maxColVal = 4;
kRow = randi([minRowVal maxRowVal]) ;
kCol = randi([minColVal maxColVal]);
table(sort(randsample(size(table,1),kRow)),sort(randsample(size(table,2),kCol))
按照给定尺寸kRow
和kCol
选择一些样本,然后从表中选择信息。