当我从矩阵元素采样时,如何知道选择了哪些元素? [英] How to know which elements have been chosen when I sample from elements of a matrix?

查看:86
本文介绍了当我从矩阵元素采样时,如何知道选择了哪些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个20x20的矩阵,其中填充了零.我已经对该矩阵进行了采样,现在有了一个带有该采样中的数字的向量.如何获取矩阵中那些采样零的坐标?

So I have a 20x20 matrix that is populated by zeroes. I have sampled this matrix and now have a vector with numbers from this sample. How do I get the coordinates of those sampled zeroes in the matrix?

a <- numeric(400)
mat <- matrix(a, 20, 20)
set.seed(1234)
sample_vector <- sample(mat, 6, replace=TRUE)

我尝试过

which (mat ==0, arr.ind=TRUE)

但是它返回矩阵中所有零的坐标(毫不奇怪),并且假设矩阵仅填充零,则不会返回采样零的坐标.

but it returns the coordinates of all the zeroes in the matrix (unsurprisingly) and given that the matrix is populated only by zeroes, it won't return the coordinates of sampled zeroes.

如何获取这些坐标?

推荐答案

问题就像从一盒400个白色球中替换掉6个球一样.除非您用唯一编号标记所有400个球,否则它们是相同的,并且无法知道绘制了哪个.

The problem is just like drawing 6 balls from a box of 400 white balls with replacement. Unless you label all 400 balls with unique numbers, they are identical and there is no way to know which have been drawn.

类似地,现在您的矩阵元素由(i,j)标记.您可以直接采样位置:

Similarly, now your matrix elements are labelled by (i,j). You may sample locations directly:

i <- sample(20, 6, TRUE)
j <- sample(20, 6, TRUE)

然后被采样的元素是mat[cbind(i,j)].

这篇关于当我从矩阵元素采样时,如何知道选择了哪些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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