- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个包含 100 棵树的树龄的向量。然后我将这些树老化到 future 5、10、15 和 20 年,以创建今年和 future 四个 5 年规划期的树龄矩阵。
但后来,我决定砍掉其中一些树(每个规划期仅砍伐 10 棵),记录在 T/F 值矩阵中,其中 T 被采伐而 F 未采伐(树木不能被采伐两次)。
age.vec <- sample(x = 1:150, size = 100, replace = T) # create our trees
age.mat <- cbind(age.vec, age.vec+5, age.vec + 10, age.vec + 15, age.vec + 20) # grow them up
x.mat <- matrix(data = F, nrow = 100, ncol = 5) # create the empty harvest matrix
x.mat[cbind(sample(1:100, size = 50), rep(1:5, each = 10))] <- T # 10 trees/year harvested
那么,那一年被采伐的树的年龄变为零: age.mat[x.mat] <- 0
然后我想在接下来的时间里再次陈化采伐的树木。例如。如果一棵树在第一个规划期收获,在第二个规划期(5 年后),我希望树的年龄为 5,然后在第三个规划期(10 年后),我希望树龄为树为 10。我已在以下 for 循环中成功实现了这一点:
for (i in 2:5){ # we don't need to calculate over the first year
age.mat[,i]<-age.mat[,i-1]+5L # add 5 to previous year
age.mat[x.mat[,i],i] <- 0L # reset age of harvested trees to zero
}
这是有效的,但是,它笨重且缓慢。有没有办法更快地实现这个(即没有 for 循环)?它也是在函数内实现的,这意味着使用“应用”实际上会减慢速度,因此需要直接对其进行矢量化。这是我要迭代数千次的内容,因此速度至关重要!
谢谢!
最佳答案
@Jon Spring 的回答中 t(apply
的替代方法是 matrixStats::rowCumsums
。
library(matrixStats)
n <- 1e4L
n10 <- n/10L
age.mat <- outer(sample(150, n, TRUE), seq(0, 20, 5), "+")
x.mat <- matrix(FALSE, n, 5) # create the empty harvest matrix
# sample harvests so that no tree is harvested twice
x.mat[matrix(c(sample(n, n/2L), sample(n10:(6L*n10 - 1L)) %/% n10), n/2L)] <- TRUE
f1 <- function(age, x) {
age[x[,1],] <- 0
for (i in 2:5){ # we don't need to calculate over the first year
age[,i] <- age[,i - 1] + 5L # add 5 to previous year
age[x[,i], i] <- 0L # reset age of harvested trees to zero
}
age
}
f2 <- function(age, x) {
age - rowCumsums(x*age)
}
microbenchmark::microbenchmark(f1 = f1(age.mat, x.mat),
f2 = f2(age.mat, x.mat),
check = "equal")
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> f1 294.4 530.2 1023.450 566.6 629.35 33222.8 100
#> f2 135.2 263.6 334.622 284.2 307.15 4343.6 100
关于r - 向量化更改矩阵列的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72611249/
我想使用Tensorflow的transform_graph工具优化图形。我尝试优化 MultiNet 中的图表(以及其他具有类似编码器-解码器架构的)。然而,优化后的图在使用 quantize_we
我试图在 C# 中将图像量化为 10 种颜色,但在绘制量化图像时遇到问题,我已经制作了映射表并且它是正确的,我已经制作了原始图像的副本并且正在更改基于映射表的像素颜色,我使用下面的代码: bm = n
我需要降低UIImage的颜色深度,但是我不知道该怎么做。结果应与在Photoshop中应用索引颜色相同。 我的目标是要具有较低的色深(32色或更低)。也许这是我的错,但我没有找到解决方法。令人惊讶的
我有大量的 numpy 向量,每个形状 (3,) 都有 8 位整数值: vec = np.random.randint(2**8, size=3) 我想通过一些已知的缩减因子将这些向量量化到更小的空间
我正在用 jQuery 编写一个心理学应用程序。我的项目的一部分需要测量用户对声音的 react 时间(用户按下一个键)。因此,我需要在调用(&时间戳)声音文件和实际开始播放之间的延迟尽可能小地播放声
最近,我开始使用 Tensorflow + Keras 创建神经网络,我想尝试 Tensorflow 中提供的量化功能。到目前为止,使用 TF 教程中的示例进行试验效果很好,我有这个基本的工作示例(来
使用当前的 Tensorflow quantization ops ,我将如何在推理过程中模拟每 channel 量化?这paper将每层量化定义为 We can specify a single q
我已经卡住了我的模型并获得了 .pb 文件。然后我在 Linux 上使用 tocoConverter 量化我的模型,因为 Windows 不支持它。我有 quantized_model.tflite。
我将 git 用于一个稍微不寻常的目的——它在我写小说时存储我的文本。 (我知道,我知道......令人讨厌。) 我正在尝试跟踪生产力,并想衡量后续提交之间的差异程度。作家代表“作品”的是“文字”,至
quantization有什么区别和 simplification ? 量化是另一种简化方式吗? 在某些情况下使用量化更好吗? 或者我应该同时使用两者? 最佳答案 几何体的总大小由两个因素控制:点数和
扎克伯格说,Llama3-8B还是太大了,不适合放到手机中,有什么办法? 量化、剪枝、蒸馏,如果你经常关注大语言模型,一定会看到这几个词,单看这几个字,我们很难理解它们都干了些什么,但
相对于
我正在将一些我无法控制的 XML 转换为 XHTML。 XML 模式定义了一个 段落标记和 和 用于列表。 我经常在这个 XML 中找到嵌套在段落中的列表。因此,直接转换会导致 s 嵌套在 中s,
我看到过这样的说法:CNN 的更深层次可以学习识别更复杂的特征。这通常附带一张早期过滤器识别直线/简单曲线的图片,以及后期过滤器识别更复杂图案的图片。它具有直观意义:您距离数据越远,您对数据的理解就越
在使用 C++ 的带有 tensorflow lite 的树莓派上,对象检测无法正常工作。我的代码编译并运行,但输出似乎从未得到正确填充。我是否会遗漏任何依赖项或错误地访问结果? 我遵循了以下教程:
如何衡量/量化 Corona SDK 游戏应用中的“迟缓”? 我在我构建的基于 Corona SDK 的物理游戏(使用 Box2D)上寻找旧手机(例如 iPhone 4、Samsung GT-I900
我正在尝试创建一个 Tensorflow 量化模型,以便使用 Coral USB 加速器进行推理。这是我的问题的一个最小的独立示例: import sys import tensorflow as t
我有一个分位数回归模型,其中包含 1 个回归变量和 1 个回归变量。我想假设检验回归量在每个分位数上都相等。我想到的一种方法是在 {0.01,0.02,....,0.99} 上测试所有 tau。但是,
要求做,在 PGM 文件上使用 KMeans 进行 vector 量化(或图像压缩) 图像是 PMG 文件,其中 b = block 大小,k = 次数,t = 迭代,-g = 初始质心 图像是这样的
我是一名优秀的程序员,十分优秀!