R 中的循环及其替代方案

发布于 2024-11-26 19:25:32 字数 504 浏览 0 评论 0原文

我有一个矩阵 (1051*1051),它的对角线上有 0zero,其他地方的值都大于 0zero。目标是有条件地重新分配矩阵中的某些值。例如,我想要实现的标准是这样的:如果任何元素大于 400,那么该行/列元素将被分配一个 0zero 值。

这就是我现在的代码的设置方式:

dl <- 400       # condition

for( i in 1:dim(DIST)[1] ) {
    for( j in 1:dim(DIST)[1] ) {
        if( DIST[i,j] > dl ) {
             DIS[i,j] <- 0
        }
    }
}

DIST 是原始矩阵(1051*1051)。

DIS 是 DIST 的副本,需要进行编辑。

我的问题: 还有其他方法可以做到这一点吗?更快的方法?

我读到应该避免 R 中的循环。如果有人有更有效的方法请分享。

谢谢。

I have a matrix (1051*1051) that has 0zeros along its diagonal and values greater than 0zero everywhere else. The goal is to conditionally reassign some values in the matrix. For example, the criteria I would like to implement is this: If any element is greater than, say, 400, then that row/column element will be assigned a 0zero value.

This is how my code is setup as of now:

dl <- 400       # condition

for( i in 1:dim(DIST)[1] ) {
    for( j in 1:dim(DIST)[1] ) {
        if( DIST[i,j] > dl ) {
             DIS[i,j] <- 0
        }
    }
}

DIST is the original matrix (1051*1051).

DIS is the copy of DIST and to be edited.

My question:
Is there any other way to do this? A faster way?

I have read that loops in R should be avoided. If anyone has a more efficient way please share.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像极了他 2024-12-03 19:25:32

只需使用 [] 赋值:

DIST[DIST>400] <- 0

请参阅 ?'[' 了解其工作原理。关键是 DIST>400 生成长度为 length(DIST)(DIST 中的元素数量)的逻辑向量,如果元素 >400,则为 TRUE否则为 FALSE。然后使用该向量对矩阵进行子集化,并且仅分配选定的元素。

Just use [] assignment:

DIST[DIST>400] <- 0

See ?'[' for how this works. The key is that DIST>400 produces a logical vector of length length(DIST) (the number of elements in DIST), consisting of TRUE if the element is >400 and FALSE otherwise. That vector is then used to subset the matrix, and only the selected elements get assigned to.

饭团 2024-12-03 19:25:32

尝试

DIS[DIST > d1] <- 0

完整示例:

n <- 10
d1 <- 400
DIST <- matrix(as.integer(runif(n^2)*1e4), n, n)
DIS <- DIST
DIS[DIST > d1] <- 0

Try

DIS[DIST > d1] <- 0

Full example:

n <- 10
d1 <- 400
DIST <- matrix(as.integer(runif(n^2)*1e4), n, n)
DIS <- DIST
DIS[DIST > d1] <- 0
GRAY°灰色天空 2024-12-03 19:25:32

下面的示例将矩阵视为向量,并使用 row() 和 col() 函数返回条目的相关行和列大于条件。

首先,创建一些虚拟数据:

set.seed(1)
m <- matrix(runif(25), ncol = 5)
diag(m) <- 0

接下来,我们使用 row() 和 col() 返回一个矩阵,其中包含矩阵中每个条目的行或列索引。

mr <- row(m)
mc <- col(m)

mr 看起来像这样,例如:

> mr
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
[4,]    4    4    4    4    4
[5,]    5    5    5    5    5

现在我们设置条件值,并选择 m 中超出条件的单元格:

cond <- 0.95
want <- which(m > cond)

有了这些超出条件的单元格,我们提取这些单元格的唯一行索引和列索引,

rwant <- unique(mr[want])
cwant <- unique(mc[want])

这些是您想要设置为 0 的行和列。

这里我们将此设置为零,首先复制 m2 中的 m 进行比较:

m2 <- m
m2[rwant, ] <- 0
m2[, cwant] <- 0

这是两个矩阵:

> m
          [,1]       [,2]      [,3]      [,4]      [,5]
[1,] 0.0000000 0.89838968 0.2059746 0.4976992 0.9347052
[2,] 0.3721239 0.00000000 0.1765568 0.7176185 0.2121425
[3,] 0.5728534 0.66079779 0.0000000 0.9919061 0.6516738
[4,] 0.9082078 0.62911404 0.3841037 0.0000000 0.1255551
[5,] 0.2016819 0.06178627 0.7698414 0.7774452 0.0000000
> m2
          [,1]       [,2]      [,3] [,4]      [,5]
[1,] 0.0000000 0.89838968 0.2059746    0 0.9347052
[2,] 0.3721239 0.00000000 0.1765568    0 0.2121425
[3,] 0.0000000 0.00000000 0.0000000    0 0.0000000
[4,] 0.9082078 0.62911404 0.3841037    0 0.1255551
[5,] 0.2016819 0.06178627 0.7698414    0 0.0000000

Here is an example of doing this treating the matrix as a vector and using the row() and col() functions to return the relevant rows and columns of the entries that are greater than a condition.

First, create some dummy data:

set.seed(1)
m <- matrix(runif(25), ncol = 5)
diag(m) <- 0

Next we use row() and col() to return a matrix with the row or col index for each entry in the matrix.

mr <- row(m)
mc <- col(m)

mr looks like this, for example:

> mr
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
[4,]    4    4    4    4    4
[5,]    5    5    5    5    5

Now we set out condition value, and select those cells of m that exceed the condition:

cond <- 0.95
want <- which(m > cond)

Having these cells that exceed the condition, we extract the unique row and column indexes for these cells

rwant <- unique(mr[want])
cwant <- unique(mc[want])

these are the rows and columns you want setting to 0.

Here we do this setting to zero, first copying m in m2 for comparison:

m2 <- m
m2[rwant, ] <- 0
m2[, cwant] <- 0

Here are the two matrices:

> m
          [,1]       [,2]      [,3]      [,4]      [,5]
[1,] 0.0000000 0.89838968 0.2059746 0.4976992 0.9347052
[2,] 0.3721239 0.00000000 0.1765568 0.7176185 0.2121425
[3,] 0.5728534 0.66079779 0.0000000 0.9919061 0.6516738
[4,] 0.9082078 0.62911404 0.3841037 0.0000000 0.1255551
[5,] 0.2016819 0.06178627 0.7698414 0.7774452 0.0000000
> m2
          [,1]       [,2]      [,3] [,4]      [,5]
[1,] 0.0000000 0.89838968 0.2059746    0 0.9347052
[2,] 0.3721239 0.00000000 0.1765568    0 0.2121425
[3,] 0.0000000 0.00000000 0.0000000    0 0.0000000
[4,] 0.9082078 0.62911404 0.3841037    0 0.1255551
[5,] 0.2016819 0.06178627 0.7698414    0 0.0000000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文