使用索引向量对矩阵进行子集化 [英] Subsetting a matrix using a vector of indices

查看:106
本文介绍了使用索引向量对矩阵进行子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个值矩阵,一个向量告诉我,矩阵的每一行,我想访问哪一列.如何在没有循环的情况下检索这些值?

Suppose I have a matrix of values, and a vector which tells me, for each row of the matrix, which (one!) column I'd like to access. How, without a loop, do I retrieve those values?

数据:

dta <- structure(c(0.02, 0.01, 0, 0.08, 0.18, 0.01, 0.12, 0, 0.03, 0, 
                   0.95, 0.96, 0.94, 0.97, 0.98, 0.95, 0.99, 0.91, 0.96, 0.98, 
                   0.98, 0.99, 1, 0.92, 0.82, 0.99, 0.88, 1, 0.97, 1, 
                   0.05, 0.04, 0.06, 0.03, 0.02, 0.05, 0.01, 0.09, 0.04, 0.02), 
                 .Dim = c(20L, 2L), .Dimnames = list(NULL, c("1", "2")))

索引向量:

idx <- c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)

所需的输出:

0.98
0.98
0.99
0.91
0.82
0.99
0.88
0.99
0.96
0.99
0.95
0.96
0.94
0.96
0.97
0.94
0.98
0.91
0.96
0.97

背景:e1071::cmeans返回membership(指向所有群集的所有点的成员资格值)和cluster(最可能的群集).我想要一个最可能的群集的隶属度值的向量来为群集图生成透明的颜色.

Background: e1071::cmeans returns membership, the membership values of all points to all clusters, and cluster, the most probable cluster. I would like a vector of the membership values of the most probable clusters to generate transparent colors for a cluster plot.

推荐答案

如何

dta <- structure(c(0.02, 0.01, 0, 0.08, 0.18, 0.01, 0.12, 0, 0.03, 0, 
                   0.95, 0.96, 0.94, 0.97, 0.98, 0.95, 0.99, 0.91, 0.96, 0.98, 
                   0.98, 0.99, 1, 0.92, 0.82, 0.99, 0.88, 1, 0.97, 1, 
                   0.05, 0.04, 0.06, 0.03, 0.02, 0.05, 0.01, 0.09, 0.04, 0.02), 
                   .Dim = c(20L, 2L), .Dimnames = list(NULL, c("1", "2")))

idx <- c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)

dta[cbind(1:nrow(dta), idx)]

我明确地认为length(idx) == nrow(dta).如果它们不相等,则R将堆叠idx所需的次数,以使元素的数量与行的数量相同.因此,如果省略idx的最后一个元素,则会得到

I clearly assume that length(idx) == nrow(dta). If they are not equal, R will stack idx as many times as necessary to have the same amount of elements as we have rows. So if you leave out the last element of idx you get

idx <- c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)
dta[cbind(1:nrow(dta), idx)]

[1] 0.98 0.99 1.00 0.92 0.82 0.99 0.88 1.00 0.97 1.00 0.95 0.96 0.94 0.97 0.98 0.95 0.99 0.91 0.96 0.02

从第二列中选取最后一个元素,而不是一个,因为R再次从idx[1]开始.

where the last element is picked from column two instead of one since R started at idx[1] again.

这篇关于使用索引向量对矩阵进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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