在矩阵中查找范围 [英] Find ranges in Matrix

查看:96
本文介绍了在矩阵中查找范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个像这样的数字向量

There is a vector of numbers like

c(3,10,24,29,37)->k

和一张桌子

    c(1,3)->l1
    c(9,12)->l2
    c(24,27)->l3
rbind(l1,l2)->ll
rbind(ll,l3)->l3

   [,1] [,2]
l1    1    3
l2    9   12
l3   24   27

如何找到向量中哪些数字在表的范围内.在这里,结果将是3,24和10.也许只是这些数字的索引...

how can I find which of the numbers in the vector are in the range of the table. Here the result would be 3,24 and 10. Maybe just the indices of these numbers...

推荐答案

您可以使用any和条件语句尝试sapply:

You can try sapply with any and the conditional statements:

indx <- sapply(k, function(x) any(x >= l3[,1] & x <= l3[,2]))
k[indx]
[1]  3 10 24

这篇关于在矩阵中查找范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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