选择间隔之间的矩阵元素

发布于 2024-10-25 18:20:05 字数 259 浏览 3 评论 0原文

是否有任何代码可以选择间隔之间矩阵的所有元素(间隔为: min(data[,1])min(data[,dim(data)[2]] ))?例如数据是这样的:

> data <- matrix(c(58,47,40,42,38,22,53,43,36,62,51,44),byrow=T,ncol=3)

所选元素应该是:22,36,38,40,42。
非常感谢。

Is there any code to choose all elements of a matrix between interval (the interval are: min(data[,1]) and min(data[,dim(data)[2]]))? For example, the data is like this:

> data <- matrix(c(58,47,40,42,38,22,53,43,36,62,51,44),byrow=T,ncol=3)

The chosen elements should be: 22,36,38,40,42.
Many thanks in advance.

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

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

发布评论

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

评论(1

孤星 2024-11-01 18:20:05

鉴于您想要第一列的最小值和最后一列的最小值之间的所有元素,您可以直接索引矩阵:

dat <- matrix(c(58, 47, 40, 42, 38, 22, 53, 43, 36, 62, 51, 44), byrow = TRUE, ncol = 3)

## grab the two values and sort them (assumes there are no missing values)
## using ncol() is a bit neater than dim(x)[2] for a matrix
minmax <- sort(c(min(dat[,1]), min(dat[,ncol(dat)])))

## subset by direct indexing (as if dat were a vector)
res <- dat[dat >= minmax[1] & dat <= minmax[2]]

## sort the result
sort(res)
[1] 22 36 38 40 42

我将我的矩阵称为“dat”而不是“data”,因为这是 R 中的函数。

Given that you you want all elements between the minimum of the first column and the minimum of the last colum, you can index the matrix directly:

dat <- matrix(c(58, 47, 40, 42, 38, 22, 53, 43, 36, 62, 51, 44), byrow = TRUE, ncol = 3)

## grab the two values and sort them (assumes there are no missing values)
## using ncol() is a bit neater than dim(x)[2] for a matrix
minmax <- sort(c(min(dat[,1]), min(dat[,ncol(dat)])))

## subset by direct indexing (as if dat were a vector)
res <- dat[dat >= minmax[1] & dat <= minmax[2]]

## sort the result
sort(res)
[1] 22 36 38 40 42

I called my matrix "dat" rather than "data", as that is a function in R.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文