请注意:我不知道我在哪里,但我花了一整天的时间来解决这个问题。因此,我请求不要将其作为重复问题丢弃,并将其视为与矩阵结构相关的非常具体的问题。
我有以下data.frame:
function remove_bottom_up(base_arr, removed_arr) {
// Initialize memoization table
var memoized = new Array(base_arr.length + 1);
for (var i = 0; i < base_arr.length + 1; i++) {
memoized[i] = new Array(removed_arr.length + 1);
}
memoized[base_arr.length][removed_arr.length] = true;
// Calculate memoization table
for (var i1 = base_arr.length - 1; i1 >= 0; i1--) {
for (var i2 = removed_arr.length; i2 >= 0; i2--) {
if ((i2 < removed_arr.length) && (base_arr[i1] == removed_arr[i2])) {
memoized[i1][i2] = memoized[i1 + 1][i2 + 1];
}
memoized[i1][i2] |= memoized[i1 + 1][i2];
}
}
// Collect all variants
var all_variants = [];
backtrack(base_arr, removed_arr, 0, 0, memoized, [], all_variants);
return all_variants;
}
function backtrack(base_arr, removed_arr, i1, i2, memoized, stack, all_variants) {
if (!memoized[i1][i2]) {
// arr2 can't be removed from arr1
return;
}
if (i1 == base_arr.length) {
all_variants.push(stack.slice(0));
return;
}
if ((i2 < removed_arr.length) && (base_arr[i1] == removed_arr[i2])) {
backtrack(base_arr, removed_arr, i1 + 1, i2 + 1, memoized, stack, all_variants);
}
stack.push(base_arr[i1]);
backtrack(base_arr, removed_arr, i1 + 1, i2, memoized, stack, all_variants);
stack.pop();
}
console.log(JSON.stringify(remove_bottom_up([1, 2, 1, 3, 1, 4, 4], [1, 4, 4])));
console.log(JSON.stringify(remove_bottom_up([8, 6, 4, 4], [6, 4, 8])));
console.log(JSON.stringify(remove_bottom_up([1, 1, 2], [1])));
console.log(JSON.stringify(remove_bottom_up([1, 1, 1, 1, 1, 1, 1], [1, 1, 1])));
我希望它在结构上与此类似:
<script>
$(function() {
$('body').on('keydown', '#distype, #val', function(e) {
console.log(this.value);
if (e.which === 32 && e.target.selectionStart === 0) {
return false;
}
});
});
</script>
我尝试了以下内容:
dput(c.m.q)
structure(list(ASK_Price = c(1801, 1687.3, 1687.2, 1688.95, 1687.15,
1688.95, 1687.5, 1688.85, 1689, 1688.95, 1687.5, 1688, 1688,
1687.5, 1689.95, 1685.85, 1689.9, 1689.95, 1688.8, 1688.95, 1687.15,
1687.2, 1690, 1688, 1688, 1690, 1688.7, 1690, 1688.7, 1689.9,
1688.7, 1689.9, 1688.7, 1689.9, 1689.8, 1687.95, 1689.75, 1689.7,
1687.95, 1689.35, 1689.3), BID_Price = c(1651, 1688.5, 1687,
1688.5, 1687, 1688.5, 1687.05, 1688.5, 1687.05, 1687.05, 1688.5,
1688.5, 1688.5, 1688.5, 1688.7, 1685.35, 1688.2, 1688.95, 1685.6,
1685.6, 1689.05, 1685.8, 1689.1, 1686.1, 1686.25, 1689.95, 1688.05,
1689.95, 1688.1, 1689.8, 1688.1, 1689.8, 1686.1, 1689.8, 1689.7,
1686.15, 1689.7, 1689.15, 1686.15, 1689.15, 1689.15)), .Names = c("ASK_Price",
"BID_Price"), row.names = c(1L, 17704L, 17707L, 17708L, 17709L,
17710L, 17713L, 17714L, 17717L, 17720L, 17729L, 17732L, 17735L,
17738L, 17983L, 17984L, 17985L, 17986L, 18015L, 18018L, 18029L,
18032L, 18033L, 18223L, 18225L, 18226L, 18227L, 18228L, 18229L,
18230L, 18231L, 18232L, 18233L, 18234L, 18235L, 18236L, 18237L,
18238L, 18239L, 18240L, 18241L), class = "data.frame")
但我没有得到类似的矩阵结构:
dput(mydata)
structure(c(4.56, 4.57, 4.53, 4.59, 4.55, 4.59, 4.59, 4.55, 4.55,
4.55, 4.59, 4.55, 4.59, 4.59, 4.7, 4.64, 4.65, 4.66, 4.65, 4.66,
4.66, 4.65, 4.65, 4.65, 4.66, 4.65, 4.66, 4.66), .Dim = c(14L, 2L))
答案 0 :(得分:2)
我们可以在转换为dimnames
后将matrix
设置为NULL(假设OP的帖子中显示的预期输出没有任何dimnames)
m1 <- `dimnames<-`(as.matrix(c.m.q), NULL)
如果我们检查&#39; m1&#39;的dput
,则结构类似
dput(m1)
structure(c(1801, 1687.3, 1687.2, 1688.95, 1687.15, 1688.95,
1687.5, 1688.85, 1689, 1688.95, 1687.5, 1688, 1688, 1687.5, 1689.95,
1685.85, 1689.9, 1689.95, 1688.8, 1688.95, 1687.15, 1687.2, 1690,
1688, 1688, 1690, 1688.7, 1690, 1688.7, 1689.9, 1688.7, 1689.9,
1688.7, 1689.9, 1689.8, 1687.95, 1689.75, 1689.7, 1687.95, 1689.35,
1689.3, 1651, 1688.5, 1687, 1688.5, 1687, 1688.5, 1687.05, 1688.5,
1687.05, 1687.05, 1688.5, 1688.5, 1688.5, 1688.5, 1688.7, 1685.35,
1688.2, 1688.95, 1685.6, 1685.6, 1689.05, 1685.8, 1689.1, 1686.1,
1686.25, 1689.95, 1688.05, 1689.95, 1688.1, 1689.8, 1688.1, 1689.8,
1686.1, 1689.8, 1689.7, 1686.15, 1689.7, 1689.15, 1686.15, 1689.15,
1689.15), .Dim = c(41L, 2L))