- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个矩阵,如:
df<-data.frame(a=c(1,2,5,4,5,4), b=c(3,4,8,6,7,4))
df1<-data.frame(a=c(5,4), b=c(7,4))
which( df ==df1[1,1], arr.ind=T )
(5,1;6,2)
最佳答案
Rcpp 是解决此类问题的好工具。
我在这里有点过火,写了一个非常复杂的函数,它可以找到任何维度的较小数组中较小数组的所有匹配项的最低索引(矩阵的左上角)角的坐标。如果你想在一个 11 维数组中找到一个 9 维数组的所有位置,这个函数可以为你做。
这里是:
library('Rcpp');
cppFunction('
IntegerMatrix findarray(IntegerVector big, IntegerVector small, bool nacmp=true ) {
// debugging macros
#define QUOTEID(...) #__VA_ARGS__
#define QUOTE(...) QUOTEID(__VA_ARGS__)
#define PRINT_VEC(vec,...) Rprintf(QUOTE(vec)"={"); if (vec.size() > 0) { Rprintf("%ld",vec[0]); for (size_t i = 1; i < vec.size(); ++i) Rprintf(",%ld",vec[i]); } Rprintf("}"__VA_ARGS__);
typedef std::vector<size_t> Dims;
// get big dimensions, treating a plain vector as a 1D array
Dims bigdims;
SEXP bigdimsSE = big.attr("dim");
if (Rf_isNull(bigdimsSE)) {
bigdims.push_back(big.size());
} else {
bigdims = as<Dims>(bigdimsSE);
}
//PRINT_VEC(bigdims,"\\n");
// now we can use this macro to easily return a result matrix with no matches
#define RES_NOMATCH IntegerMatrix(0,bigdims.size())
// get small dimensions, treating a plain vector as a 1D array
Dims smalldims;
SEXP smalldimsSE = small.attr("dim");
if (Rf_isNull(smalldimsSE)) {
smalldims.push_back(small.size());
} else {
smalldims = as<Dims>(smalldimsSE);
}
//PRINT_VEC(smalldims,"\\n");
// trivial case: if small has greater dimensionality than big, just return no matches
// note: we could theoretically support this case, at least when all extra small dimensions have only one index, but whatever
if (smalldims.size() > bigdims.size())
return RES_NOMATCH;
// derive a "bounds" Dims object, which will represent the maximum index plus one in big against which we must compare the first index in small for the corresponding dimension
// if small is greater than big in any dimension, then we can return no matches immediately
Dims bounds(smalldims.size());
for (size_t i = 0; i < smalldims.size(); ++i) {
if (smalldims[i] > bigdims[i])
return RES_NOMATCH;
bounds[i] = bigdims[i]-smalldims[i]+1;
}
// trivial case: if either big or small has any zero-length dimension, then just return no matches, because in that case the offending argument cannot have any actual data in it
// theoretically you can consider such degenerate arrays to match everywhere, sort of like the empty string matching at every position in any given string, but whatever
for (size_t i = 0; i < bigdims.size(); ++i) if (bigdims[i] == 0) return RES_NOMATCH;
for (size_t i = 0; i < smalldims.size(); ++i) if (smalldims[i] == 0) return RES_NOMATCH;
// prepare to build up the result data
// it would not make sense to build up the result data directly in a matrix, because we have to add one row at a time, which does not commute with the internal storage arrangement of matrices
// I then tried to use a data.frame, but the Rcpp DataFrame type is surprisingly light in functionality, seemingly without any provision for adding a row, and requires named columns, so best to avoid that
// instead, we\'ll just build up the data on a vector of vectors, going all-STL
typedef std::vector<std::vector<int> > ResBuilder;
ResBuilder resBuilder(bigdims.size());
// retrieve raw vector pointers for best performance
int* bigp = INTEGER(big);
int* smallp = INTEGER(small);
// now, iterate through each index of each (big) dimension from zero through the bound for that dimension (which is automatically the big dimension\'s length if small\'s dimensionality does not extend to that dimension), and see if small\'s first element matches
Dims bdis(bigdims.size()); // conveniently, initializes to all zeroes
size_t bvi = 0; // big vector index
while (true) { // big element loop, restricted to bounds
if (bigp[bvi] == smallp[0] && (nacmp || bigp[bvi] != NA_INTEGER)) {
//PRINT_VEC(bdis," ") Rprintf("found first element match at bvi=%ld big=small=%d\\n",bvi,bigp[bvi]);
size_t bvi2 = bvi; // don\'t screw up the original bvi; matches can overlap
// now we need to iterate through each index of each (small) dimension and test if all remaining elements match
Dims sdis(smalldims.size()); // conveniently, initializes to all zeroes
size_t svi = 0;
bool match = true; // assumption
while (true) { // small element loop
// note: once inside this inner loop, we don\'t have to worry about bounds anymore, because we already enforced that the outer loop will only iterate over indexes within bounds
// increment small and big indexes
++svi; // always increment svi by exactly one; the small array governs this matching loop
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("incremented svi=%ld\\n",svi);
size_t bm = 1;
size_t d;
for (d = 0; d < sdis.size(); ++d) {
++sdis[d];
++bvi2;
if (sdis[d] == smalldims[d]) {
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("reached small end=%ld of dimension d=%ld; bvi2=%ld bm=%ld\\n",smalldims[d],d,bvi2,bm);
sdis[d] = 0;
bvi2 += (bigdims[d]-smalldims[d])*bm-1;
bm *= bigdims[d];
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("after jumping to next index we have bvi2=%ld bm=%ld\\n",bvi2,bm);
} else {
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("valid dimension index increment at dimension d=%ld; bvi2=%ld bm=%ld\\n",d,bvi2,bm);
break;
}
}
// test if we reached the end of small; then break the inner while loop, and we have a match
if (d == sdis.size())
break;
// at this point, we have a new element to test; if unequal, we have no match
if (bigp[bvi2] != smallp[svi] || !nacmp && bigp[bvi] == NA_INTEGER) {
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("match overturned by big=%d != small=%d\\n",bigp[bvi2],smallp[svi]);
match = false;
break;
} else {
//PRINT_VEC(bdis," ") PRINT_VEC(sdis," ") Rprintf("match respected by big=small=%d\\n",bigp[bvi2]);
}
}
// if we have a match, add it to the result data
if (match) {
//PRINT_VEC(bdis," ") Rprintf("found complete match!\\n");
for (size_t bd = 0; bd < bigdims.size(); ++bd)
resBuilder[bd].push_back(bdis[bd]+1); // also add one to convert from C++ zero-based to R one-based indexes
//PRINT_VEC(bdis," ") Rprintf("resBuilder dims = {%ld,%ld}\\n",resBuilder[0].size(),resBuilder.size());
}
} else {
//PRINT_VEC(bdis," ") Rprintf("first element mismatch: big=%d != small=%d\\n",bigp[bvi],smallp[0]);
}
// increment big index
size_t bm = 1;
size_t d;
for (d = 0; d < bdis.size(); ++d) {
++bdis[d];
++bvi;
size_t bound = bounds.size() > d ? bounds[d] : bigdims[d];
if (bdis[d] >= bound) {
//PRINT_VEC(bdis," ") Rprintf("big index hit bound=%ld of dimension d=%ld; bvi=%ld bm=%ld\\n",bound,d,bvi,bm);
bdis[d] = 0;
bvi += (bigdims[d]-bound)*bm-1;
bm *= bigdims[d];
//PRINT_VEC(bdis," ") Rprintf("after advancing big index we have bvi=%ld bm=%ld\\n",bvi,bm);
} else {
//PRINT_VEC(bdis," ") Rprintf("valid dimension index increment at dimension d=%ld; bvi=%ld bm=%ld\\n",d,bvi,bm);
break;
}
}
// test if we reached the end of big; then break the outer while loop, and we\'re done
if (d == bdis.size() || bvi >= big.size())
break;
}
// copy to a matrix
IntegerMatrix res(resBuilder[0].size(),resBuilder.size());
int* resp = INTEGER(res);
for (size_t c = 0; c < res.ncol(); ++c)
std::copy(resBuilder[c].begin(),resBuilder[c].end(),resp+c*res.nrow());
// return the matrix
return res;
}
');
big
数组,然后是
small
数组,然后是结果,最后是一个逻辑向量,测试大小为
small
的切片是否从每个连续的
big
中的匹配实际上与
small
相同):
## testing
slice <- function(arr,is,ls,...) { length(ls) <- length(is); ls[is.na(ls)] <- 1; do.call(`[`,c(list(arr),Map(function(i,l) seq(i,len=l),is,ls),...)); };
printAndTest <- function(big,small) { print(big); print(small); findarray(big,small); };
printAndTestAndSliceIdentical <- function(big,small) { big <- structure(as.integer(big),dim=dim(big)); small <- structure(as.integer(small),dim=dim(small)); res <- printAndTest(big,small); print(res); if (nrow(res) > 0) sapply(1:nrow(res),function(r) identical(structure(slice(big,res[r,],if (is.null(dim(small))) length(small) else dim(small),drop=F),dim=dim(small)),small)) else logical(); };
## one-element match
printAndTestAndSliceIdentical(1,1);
## [1] 1
## [1] 1
## [,1]
## [1,] 1
## [1] TRUE
## vector in vector
printAndTestAndSliceIdentical(1:3,2:3);
## [1] 1 2 3
## [1] 2 3
## [,1]
## [1,] 2
## [1] TRUE
printAndTestAndSliceIdentical(1:3,1:3);
## [1] 1 2 3
## [1] 1 2 3
## [,1]
## [1,] 1
## [1] TRUE
printAndTestAndSliceIdentical(1:3,1:4);
## [1] 1 2 3
## [1] 1 2 3 4
## [,1]
## logical(0)
## vector in matrix
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),1:2);
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [1] 1 2
## [,1] [,2]
## [1,] 1 1
## [2,] 1 4
## [1] TRUE TRUE
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),12);
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [1] 12
## [,1] [,2]
## [1,] 4 3
## [2,] 4 6
## [1] TRUE TRUE
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),5:8);
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [1] 5 6 7 8
## [,1] [,2]
## [1,] 1 2
## [2,] 1 5
## [1] TRUE TRUE
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),5:9);
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [1] 5 6 7 8 9
## [,1] [,2]
## logical(0)
## matrix in matrix
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),matrix(1:4,2));
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [,1] [,2]
## logical(0)
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),matrix(c(2,3,6,7),2));
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [,1] [,2]
## [1,] 2 6
## [2,] 3 7
## [,1] [,2]
## [1,] 2 1
## [2,] 2 4
## [1] TRUE TRUE
printAndTestAndSliceIdentical(matrix(rep(1:12,2),4),matrix(c(7,8,11,12),2));
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 5 9 1 5 9
## [2,] 2 6 10 2 6 10
## [3,] 3 7 11 3 7 11
## [4,] 4 8 12 4 8 12
## [,1] [,2]
## [1,] 7 11
## [2,] 8 12
## [,1] [,2]
## [1,] 3 2
## [2,] 3 5
## [1] TRUE TRUE
## vector in cube
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),1);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 1
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),8);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 8
## [,1] [,2] [,3]
## [1,] 4 2 1
## [2,] 4 2 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),9);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 9
## [,1] [,2] [,3]
## [1,] 1 3 1
## [2,] 1 3 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),12);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 12
## [,1] [,2] [,3]
## [1,] 4 3 1
## [2,] 4 3 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),1:4);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 1 2 3 4
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),1:5);
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [1] 1 2 3 4 5
## [,1] [,2] [,3]
## logical(0)
## matrix in cube
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),matrix(c(7,8,11,12),2));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [,1] [,2]
## [1,] 7 11
## [2,] 8 12
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 3 2 2
## [1] TRUE TRUE
printAndTestAndSliceIdentical(array(1:12,c(4,3,2)),matrix(c(7,8,11,11),2));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## [,1] [,2]
## [1,] 7 11
## [2,] 8 11
## [,1] [,2] [,3]
## logical(0)
## cube in cube
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(c(1,13,25),c(1,1,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1]
## [1,] 1
##
## , , 2
##
## [,1]
## [1,] 13
##
## , , 3
##
## [,1]
## [1,] 25
##
## [,1] [,2] [,3]
## [1,] 1 1 1
## [1] TRUE
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(c(6,18,30),c(1,1,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1]
## [1,] 6
##
## , , 2
##
## [,1]
## [1,] 18
##
## , , 3
##
## [,1]
## [1,] 30
##
## [,1] [,2] [,3]
## [1,] 2 2 1
## [1] TRUE
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(c(18,30),c(1,1,2)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1]
## [1,] 18
##
## , , 2
##
## [,1]
## [1,] 30
##
## [,1] [,2] [,3]
## [1,] 2 2 2
## [1] TRUE
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(1:36,c(4,3,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## [,1] [,2] [,3]
## [1,] 1 1 1
## [1] TRUE
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(c(7,8,11,12,19,20,23,24,31,32,35,36),c(2,2,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1] [,2]
## [1,] 7 11
## [2,] 8 12
##
## , , 2
##
## [,1] [,2]
## [1,] 19 23
## [2,] 20 24
##
## , , 3
##
## [,1] [,2]
## [1,] 31 35
## [2,] 32 36
##
## [,1] [,2] [,3]
## [1,] 3 2 1
## [1] TRUE
printAndTestAndSliceIdentical(array(1:36,c(4,3,3)),array(c(7,8,11,12,19,20,23,24,31,32,35,37),c(2,2,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1] [,2]
## [1,] 7 11
## [2,] 8 12
##
## , , 2
##
## [,1] [,2]
## [1,] 19 23
## [2,] 20 24
##
## , , 3
##
## [,1] [,2]
## [1,] 31 35
## [2,] 32 37
##
## [,1] [,2] [,3]
## logical(0)
printAndTestAndSliceIdentical(array(1:36,c(4,3,6)),array(c(7,8,11,12,19,20,23,24,31,32,35,36),c(2,2,3)));
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 5
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
##
## , , 6
##
## [,1] [,2] [,3]
## [1,] 25 29 33
## [2,] 26 30 34
## [3,] 27 31 35
## [4,] 28 32 36
##
## , , 1
##
## [,1] [,2]
## [1,] 7 11
## [2,] 8 12
##
## , , 2
##
## [,1] [,2]
## [1,] 19 23
## [2,] 20 24
##
## , , 3
##
## [,1] [,2]
## [1,] 31 35
## [2,] 32 36
##
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 3 2 4
## [1] TRUE TRUE
df <- data.frame(a=c(1,2,5,4,5,4),b=c(3,4,8,6,7,4));
df1 <- data.frame(a=c(5,4),b=c(7,4));
findarray(as.matrix(df),as.matrix(df1));
## [,1] [,2]
## [1,] 5 1
small
的大小来推导出最高索引坐标,如下所示:
t(t(findarray(as.matrix(df),as.matrix(df1)))+dim(df1))-1;
## [,1] [,2]
## [1,] 6 2
df1
的两个维度具有相同的长度,因此无论如何都无关紧要,但在一般情况下很重要。
set.seed(12);
big <- array(sample(1:4,factorial(11),replace=T),11:1);
small <- array(sample(1:4,12,replace=T),c(2,3,2,rep(1,9-3)));
res <- findarray(big,small);
res;
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 6 6 5 3 1 3 4 3 2 1 1
## [2,] 7 7 6 3 5 6 5 4 3 2 1
sapply(1:nrow(res),function(r) identical(structure(slice(big,res[r,],dim(small),drop=F),dim=dim(small)),small));
## [1] TRUE TRUE
findarray()
是否可以找到它们。
set.seed(96);
d <- 11;
big <- array(sample(1:4,factorial(d),replace=T),d:1);
for (i in 1:5) {
is <- sapply(d:1,sample,1);
ls <- mapply(function(i,dl) sample(dl-i+1,1),is,d:1);
small <- slice(big,is,ls,drop=F);
res <- findarray(big,small);
print(rbind(is,ls,res));
};
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## is 7 6 1 4 7 2 2 3 3 1 1
## ls 3 1 2 1 1 1 1 2 1 1 1
## 5 3 6 8 4 4 4 2 1 1 1
## 7 6 1 4 7 2 2 3 3 1 1
## 8 10 7 5 1 2 2 3 1 2 1
## 9 6 3 4 4 1 4 3 3 2 1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## is 10 10 2 4 5 6 3 1 3 2 1
## ls 2 1 3 4 1 1 3 1 1 1 1
## 10 10 2 4 5 6 3 1 3 2 1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## is 8 5 5 8 2 1 5 4 1 1 1
## ls 2 1 1 1 2 3 1 1 1 1 1
## 8 5 5 8 2 1 5 4 1 1 1
## 1 4 3 1 5 1 2 1 3 1 1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## is 7 10 7 7 6 3 5 4 3 2 1
## ls 2 1 1 2 2 2 1 1 1 1 1
## 7 10 7 7 6 3 5 4 3 2 1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## is 3 8 5 1 6 3 1 3 3 2 1
## ls 9 1 2 7 2 3 4 1 1 1 1
## 3 8 5 1 6 3 1 3 3 2 1
关于r - 在矩阵中查找子矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31330196/
我有这个 html 代码: HELLO WORLD! X V HELLO WORLD! X V 我想按 X(类关闭)将父 div 的高度更改为 20px 并显示 V(类打开),但在每个 d
在会计应用程序的许多不同实现中,有两种主要的数据库设计方法来保存日志和分类帐数据。 只保留 Journal 信息,然后 Ledger 只是 Journal 的一个 View (因为 journal 总
我想在另一个子里面有一个子, sub a { sub b { } } 我想为每次调用 sub b 创建一个新的 sub a 实例。有没有办法在 Perl 中做到这一点? 当我运行上面的
我有一些代码正在查找重复项并突出显示单元格: Private Sub cmdDups_Click() Dim Rng As Range Dim cel As Range Set Rng = ThisW
可能有一个简单的解决方案,但我很难过。 我有一个包含一个 ID 字段的主表。在两个可能的字段中有一个具有该 ID 的子表。想象一个由选手 A 和选手 B 组成的 double 队。Master 表将有
假设我有一个包含对象的数组: [ { "id": "5a97e047f826a0111b754beb", "name": "Hogwarts", "parentId": "
我正在尝试对 MySQL 数据库表执行一对父/子模型的批量插入,但似乎无法使用标准的 ActiveRecord 功能来完成。所以,我尝试了 activerecord-import gem,但它也不支持
我有一个带有多个子类的父抽象类。最终,我希望通过 GUI 中的进度条显示子类中完成的进度。 我目前所做的,我意识到这是行不通的,是在父类中声明为每个子类将覆盖的虚拟方法的事件方法定义。所以像: pub
是否可以通过键数组在对象中设置变量?例如我有这个对象: var obj = {'outer': {'inner': 'value'} }; 并希望设置由键数组选择的值: var keys = ['ou
我有一个名为 companies 的 MySQL 表,如下所示: +---------+-----------+-----------+ | id_comp | comp_name | id_pare
我正在尝试使用 sublime text 在 sublime text 上的 ionic 上打开我的第一个应用程序。它给了我一个“找不到命令”的错误。如何修复? 我试过这些命令: sudo rm -r
不好意思问,但我正在使用 webapp2,我正在设计一个解决方案,以便更容易定义路由 based on this google webapp2 route function .但这完全取决于能够在子级
我有代表树的数字字符串(我不知道是否有官方名称): 012323301212 上面的例子代表了 2 棵树。根用 0 表示。根的直接子代为“1”,“1”的直接子代为“2”,依此类推。我需要将它们分组到由
是否可以在当前 Activity 之上添加 Activity 。例如,假设我单击一个按钮,然后它将第二个 Activity 添加到当前 Activity 。而第二个 Activity 只覆盖了我当前
我很难思考如何为子资源建模。 以作者的书籍为例。你可以有 N 本书,每本书只有一位作者。 /books GET /books POST /books/id PUT /books/id DELETE 到
有人可以向我解释以下内容(python 2.7) 来自已解析文件的两个字符串数字: '410.9''410.9 '(注意尾随空格) A_LIST = ['410.9 '] '410.9' in '41
背景 在 PowerShell 中构建 hash table 是很常见的通过特定属性快速访问对象,例如以 LastName 为基础建立索引: $List = ConvertFrom-Csv @' I
我真的很难弄清楚如何调用嵌套 Polymer Web 组件的函数。 这是标记: rise-distribution组件有 canPlay我想从 rise-playlist
我写了一个小工具转储(以 dot 格式)一个项目的依赖关系图,其中所有位于同一目录中的文件都聚集在一个集群中。当我尝试生成包含相应图形的 pdf 时,dot开始哭: 命令 dot -Tpdf trim
给定一个 CODE ref,是否可以: 访问该 CODE ref 的解析树 通过指定 CODE ref 的解析树来创建一个新的 CODE ref,该解析树可以包含在 1 中返回的解析树的元素 通常我们
我是一名优秀的程序员,十分优秀!