如何检查矩阵中的每个值是否在R中其他两个矩阵的相应值之间? [英] How to check if each value in a matrix is between the corresponding values in two other matrices in R?

查看:117
本文介绍了如何检查矩阵中的每个值是否在R中其他两个矩阵的相应值之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,希望与其他两个矩阵进行比较,以查看矩阵中的每个条目是否包含在其他两个矩阵的值之内或该值之外.例如,如果我的矩阵是:

I have a matrix that I would like to compare with two other matrices to see if each entry in my matrix is contained within the values of the other two matrices or outside the values. For example, if my matrix is:

> M
           [,1]       [,2]       [,3]
[1,] -0.1278982  0.4600544  1.3271033
[2,] -0.1079272  1.2196851 -0.5240536 
[3,]  0.9548449 -0.9979668 -0.6636296 

而我拥有的另外两个矩阵是L和U:

and the other two matrices I have are L and U:

> L 
            [,1]      [,2]       [,3]
[1,] -0.49416022 1.0992247 -1.2834469
[2,]  0.35491793 0.2460946  1.1411529
[3,]  0.01755317 0.2469062  0.2843848

> U
            [,1]       [,2]        [,3]
[1,] -0.84996737  1.0036554 -0.04747612
[2,]  0.35396050 -0.4612527 -1.47341334
[3,] -0.07716839 -1.5921625  1.62187501

我想对M的每个条目进行比较,以查看它是否包含在L和U的相应条目所构成的范围内.

I would like to take each entry of M and compare to see if it is contained within the range made by the corresponding entry of L and U.

例如,对于M [1,1],它在-0.49416022和-0.84996737的范围之外,因此我将为该比较指定值0.另一方面,对于M [2,3] ,该值为-0.5240536,因此介于1.1411529和-1.47341334之间,因此此比较的值为0.

For example, for M[1,1], it is outside the range of -0.49416022 and -0.84996737, and so I will assign this comparison a value of 0. On the other hand, for M[2,3], the value is -0.5240536 and so is between 1.1411529 and -1.47341334, and so this comparison gets a value of 0.

最终,我想获得一个全为0和1的矩阵,指示M中的条目是否包含在L和U中的对应值中.例如,如果我们将此矩阵称为M_comparisons,则:

Ultimately, I would like to obtain a matrix of all 0 and 1's, indicating if the entries in M were contained within corresponding values in L and U. For example, if we call this matrix M_comparisons, then:

> M_comparisons
            [,1]      [,2]       [,3]
[1,]           0         0          0
[2,]           0         0          1
[3,]           0         1          0

有人对我如何在大型矩阵中以及如何在非常快的计算时间内实现这一点有任何想法吗?谢谢!

Does anyone have any ideas how I can do this for large matrices and possible in a very fast computational time? Thanks!

推荐答案

或者另一种方法是

+(M > U & M < L)

这篇关于如何检查矩阵中的每个值是否在R中其他两个矩阵的相应值之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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