从坐标列表中在矩阵中分配 1 [英] Assign 1 in a matrix from a list of coordinates

查看:17
本文介绍了从坐标列表中在矩阵中分配 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取坐标列表.对于坐标中的每个位置,我想为矩阵分配 1.最终矩阵应如下所示.我想使用快速矢量化方法而不是 for 循环.

I want to take a list of coordinates. For every location in the coordinates, I want to assign 1 to the matrix. The final matrix should look like this. I want to use a fast vectorized method instead of a for loop.

    > sample.matrix
      A B C D E F G H I J K L M N O P Q R S T
    A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
    G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    H 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    I 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    J 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    L 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    P 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
    Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    S 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    T 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

示例数据:

    set.seed(15)
    dat <- data.frame(x=sample(LETTERS[1:20], 15,set.seed(15)),y=sample(LETTERS[1:20], 15,set.seed(15)), stringsAsFactors = FALSE)
    sample.matrix <- matrix(data=0, nrow = 20, ncol = 20, dimnames = list(LETTERS[1:20],LETTERS[1:20]))

我尝试过的方法奏效了.

What I tried that worked.

    for ( n in 1:15){
      sample.matrix[dat[n,]$x,dat[n,]$y] =1
    }

我尝试过的方法不起作用.

What I tried that isn't working.

by(dat, 1:15, function(a.row){sample.matrix[a.row$x, a.row$y]=1})

推荐答案

这是该语言的一个很酷的编程方面.在 R 中,我们可以使用两列矩阵进行子集:

This a cool programming aspect of the language. In R we can subset using a two column matrix:

sample.matrix[as.matrix(dat)] <- 1

<小时>

更多关于它是如何工作的

来自 ?Extract 的帮助(我强调的是粗体字):

From the help for ?Extract (bolded text is my emphasis):

第三种形式的索引是通过一个包含一列的数字矩阵对于每个维度:索引矩阵的每一行然后选择一个数组的单个元素,结果是一个向量.消极的索引矩阵中不允许使用索引.NA 和零值是允许:包含零的索引矩阵的行被忽略,而包含 NA 的行在结果中产生 NA.

A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector. Negative indices are not allowed in the index matrix. NA and zero values are allowed: rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.

通过每维一列的字符矩阵进行索引是如果数组具有维度名称,也支持.和数字一样矩阵索引,索引矩阵的每一行选择一个元素的阵列.索引与适当的维度匹配名称.NA 是允许的,并将在结果中产生 NA.无与伦比索引以及空字符串 ("") 是不允许的,并且会导致错误.

Indexing via a character matrix with one column per dimensions is also supported if the array has dimension names. As with numeric matrix indexing, each row of the index matrix selects a single element of the array. Indices are matched against the appropriate dimension names. NA is allowed and will produce an NA in the result. Unmatched indices as well as the empty string ("") are not allowed and will result in an error.

在您的情况下,您有一个矩阵,其中的字母代表维度名称.

In your case you have a matrix with letters that represent dimension names.

     x   y  
[1,] "B" "C"
[2,] "S" "S"
[3,] "L" "B"
[4,] "N" "L"
[5,] "E" "G"
[6,] "M" "A"

当我们将这个矩阵插入一组括号[as.matrix(dat)]时,求值器将检测一个矩阵并使用特殊类型的子集.第一列代表行,第二列代表列.

When we insert this matrix into a set of brackets [as.matrix(dat)], the evaluator will detect a matrix and use a special type of subsetting. The first column will represent the rows, and the second column will represent the columns.

head(sample.matrix)
  A B C D E F G H I J K L M N O P Q R S T
A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

这篇关于从坐标列表中在矩阵中分配 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆