确定矩阵的第一个数字与所有连续数字之间的差异(Determining the difference between the first number of a matrix and all succe

编程入门 行业动态 更新时间:2024-10-24 19:15:31
确定矩阵的第一个数字与所有连续数字之间的差异(Determining the difference between the first number of a matrix and all successive numbers)

我有一个矩阵列表,其第一个矩阵如下所示:

$x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 [4,] 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0

我正在尝试创建一个具有相同尺寸的新矩阵,它将指示每行的第一个数字与每个连续数字之间的差值。 我喜欢用abs()来使这个值成为这个差异的绝对值。 因此,在上面的例子中,预期的结果是:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 [3,] 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1

我知道我可以使用apply来查看第一列和最后一列之间的区别:

abs(apply(x,1,diff, lag=15)) [1] 0 -1 0 -1

但是,我不确定如何使这个迭代过程的滞后时间从1增加到15,以捕获整个矩阵的信息。 我的原始矩阵中也有一些NA,这个ID在新矩阵中返回为NA。

最终,我喜欢使用sapply解决方案将其应用于包含500个矩阵的整个列表,如示例中的矩阵。

I have a list of matrices, the first matrix of which looks as follows:

$x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 [4,] 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0

I am trying to create a new matrix of the same dimensions that will indicate the difference in value between the first number of each row and each successive number. Id like to use abs() to make this value be the absolute value of this difference. Thus, in the example above, the expected result would be:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 [3,] 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1

I know that I can use apply to see the difference between the first and last columns:

abs(apply(x,1,diff, lag=15)) [1] 0 -1 0 -1

However, I am not sure how to make this an iterative process in which the lag is increased from 1 to 15 to capture the information for the entire matrix. I also have some NAs in my original matrix, which id like returned as NA in the new matrix.

Ultimately, Id like to use the solution with sapply to apply it across my entire list comprised of 500 matrices like the one in the example.

最满意答案

我会简单地从每个原始矩阵的第一列中建立一个单独的矩阵,然后取两个矩阵的差值的abs 。 我会用一个例子在一分钟内更新:

像这样的东西:

> m <- matrix(sample(0:1,25,replace = TRUE),5,5) > m [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 1 1 [2,] 0 0 0 1 0 [3,] 1 1 1 0 1 [4,] 1 1 1 1 0 [5,] 1 0 1 0 0 > m1 <- matrix(m[,1],5,5) > m1 [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 1 1 1 1 1 [4,] 1 1 1 1 1 [5,] 1 1 1 1 1 > abs(m-m1) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 1 1 [2,] 0 0 0 1 0 [3,] 0 0 0 1 0 [4,] 0 0 0 0 1 [5,] 0 1 0 1 1

而且你可以很容易地将它转换成一个函数来应用你的矩阵列表。

I would simply build a separate matrix from the first column of each original matrix and then take the abs of the difference of the two matrices. I'll update in a minute with an example:

Something like this:

> m <- matrix(sample(0:1,25,replace = TRUE),5,5) > m [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 1 1 [2,] 0 0 0 1 0 [3,] 1 1 1 0 1 [4,] 1 1 1 1 0 [5,] 1 0 1 0 0 > m1 <- matrix(m[,1],5,5) > m1 [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 1 1 1 1 1 [4,] 1 1 1 1 1 [5,] 1 1 1 1 1 > abs(m-m1) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 1 1 [2,] 0 0 0 1 0 [3,] 0 0 0 1 0 [4,] 0 0 0 0 1 [5,] 0 1 0 1 1

And you can easily roll this into a function to apply along your list of matrices.

更多推荐

本文发布于:2023-08-07 01:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   矩阵   差异   Determining   difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!