r - R How to Write Double for Loops and Store Result in a Matrix
问题描述
I would like to store result of X[i,j].
X[i,j] = alpha[i] + beta [j]
I tried writing this double for loop but could not get it to store the result. Appreciate any help here. Thanks!
for (i in length(alpha)) {
for (j in length(beta)) {
Xij <- alpha[i] + beta[j]
matrix[i,j] <- Xij
}
}
Edit: Is there a more efficient way to do this? The for loop run is taking a long time as the dataset is huge.
解决方案
for (i in 1:length(alpha)) {
for (j in 1:length(beta)) {
Xij <- alpha[i] + beta[j]
matrix[i,j] <- Xij
}
}
推荐阅读
- python - 如何在 Django 的登录和注册表单中添加错误消息
- stripe-payments - 在设置 data.account 时,条带扩展 BankAccountListOptions 中的 Account 对象不起作用
- android - 如何测量可组合物?
- javascript - Sinon Fake Server 没有响应请求
- python - 在滚动菜单中全选后抓取
- python - 尝试训练 keras 模型时,出现错误:没有为任何变量提供梯度
- nginx - 如何通过服务器ip地址限制对prometheus的直接IP访问?
- elasticsearch - Filebeat 到 Elasticsearch:document_id 被移除(用于自定义 _id 时)
- asp.net-mvc - 是否可以将多个内容类型路由和绑定到同一个操作?
- angular - Angular Elements - 多个实例默认共享同一个服务