计算每个矩阵列的min()和max()。

huangapple go评论107阅读模式
英文:

Calculate min() and max() of each matrix column

问题

我使用以下矩阵:

dput(data) =

结构(c(269.643848985434,284.856715574861,278.687093704939,
242.00628682971,258.754844814539,257.241126745939,250.868861973286,
267.881099879742,266.277047023177,264.643696397543,283.306375294924,
278.137760534883,251.230201125145,264.389782398939,260.087893307209,
263.933225497603,284.947050362825,279.185155779123,262.722251042724,
284.903103709221,279.329203143716,256.589251384139,274.861293360591,
270.730307921767,233.265785723925,249.657887518406,247.94396802783,
264.665669724345,284.124271348119,278.701742589474,0,0,0
),dim = c(3L,11L),dimnames = list(NULL,c(& quot; & quot;,& quot; & quot;,& quot; & quot;,& quot; & quot;,
& quot; & quot;,& quot; & quot;,& quot; & quot;,& quot; & quot;,
& quot; & quot;,& quot; & quot;,& quot; gt & quot;)))

并且,对于每一列,我想找到值的范围(minmax?)并保存它们(例如在一个列表中)。

有什么简单的方法可以做到这一点?
谢谢!

英文:

I'm using the following matrix:

dput(data) =

  1. structure(c(269.643848985434, 284.856715574861, 278.687093704939,
  2. 242.00628682971, 258.754844814539, 257.241126745939, 250.868861973286,
  3. 267.881099879742, 266.277047023177, 264.643696397543, 283.306375294924,
  4. 278.137760534883, 251.230201125145, 264.389782398939, 260.087893307209,
  5. 263.933225497603, 284.947050362825, 279.185155779123, 262.722251042724,
  6. 284.903103709221, 279.329203143716, 256.589251384139, 274.861293360591,
  7. 270.730307921767, 233.265785723925, 249.657887518406, 247.94396802783,
  8. 264.665669724345, 284.124271348119, 278.701742589474, 0, 0, 0
  9. ), dim = c(3L, 11L), dimnames = list(NULL, c("", "", "", "",
  10. "", "", "", "", "", "", "gt")))

and, for each column, I'd like to find the range of values (min and max?) and save them (f.e. in a list).

What would be an easy way to do this?
Thank you!

答案1

得分: 3

我来帮你翻译代码部分:

  1. 让我引起你的注意,**matrixStats** 包。它不仅仅是语法糖,还是用C++实现的,因此非常快速。
  2. matrixStats::colRanges(m)
  3. # [,1] [,2]
  4. # 269.6438 284.8567
  5. # 242.0063 258.7548
  6. # 250.8689 267.8811
  7. # 264.6437 283.3064
  8. # 251.2302 264.3898
  9. # 263.9332 284.9471
  10. # 262.7223 284.9031
  11. # 256.5893 274.8613
  12. # 233.2658 249.6579
  13. # 264.6657 284.1243
  14. # gt 0.0000 0.0000

请注意,我只翻译了代码部分,没有包括其他内容。

英文:

Let me draw your attention to the matrixStats package. It's nut just syntactic sugar, it's also implemented in C++ and, thus, very fast.

  1. matrixStats::colRanges(m)
  2. # [,1] [,2]
  3. # 269.6438 284.8567
  4. # 242.0063 258.7548
  5. # 250.8689 267.8811
  6. # 264.6437 283.3064
  7. # 251.2302 264.3898
  8. # 263.9332 284.9471
  9. # 262.7223 284.9031
  10. # 256.5893 274.8613
  11. # 233.2658 249.6579
  12. # 264.6657 284.1243
  13. # gt 0.0000 0.0000

答案2

得分: 2

你要找的函数是 range()

range 返回一个包含所有给定参数的最小值和最大值的向量。

使用 apply() 并设置 MARGIN = 2 来迭代每一列,应用 range() 函数。然后设置适当的行名称,例如 c("min", "max")。由于你想要一个列表,可以使用 asplit() 来将结果矩阵的列拆分为向量列表。

假设你的矩阵叫做 m

  1. apply(m, MARGIN = 2, range) |>
  2. `rownames<-`(c("min", "max")) |>
  3. asplit(MARGIN = 2)
  4. # [[1]]
  5. # min max
  6. # 269.6438 284.8567
  7. # [[2]]
  8. # min max
  9. # 242.0063 258.7548
  10. # [[3]]
  11. # min max
  12. # 250.8689 267.8811
  13. # [[4]]
  14. # min max
  15. # 264.6437 283.3064
  16. # [[5]]
  17. # min max
  18. # 251.2302 264.3898
  19. # [[6]]
  20. # min max
  21. # 263.9332 284.9471
  22. # [[7]]
  23. # min max
  24. # 262.7223 284.9031
  25. # [[8]]
  26. # min max
  27. # 256.5893 274.8613
  28. # [[9]]
  29. # min max
  30. # 233.2658 249.6579
  31. # [[10]]
  32. # min max
  33. # 264.6657 284.1243
  34. # $gt
  35. # min max
  36. # 0 0
英文:

The function you're looking for is range():

> range returns a vector containing the minimum and maximum of all the given arguments.

Use apply() with MARGIN = 2 to iterate over each column, applying the range() function. Then set appropriate row names, e.g. c("min", "max"). As you want a list, use asplit() to split the columns of the resulting matrix into a list of vectors.

Assuming your matrix is called m:

  1. apply(m, MARGIN = 2, range) |>
  2. `rownames<-`(c("min", "max")) |>
  3. asplit(MARGIN = 2)
  4. # [[1]]
  5. # min max
  6. # 269.6438 284.8567
  7. # [[2]]
  8. # min max
  9. # 242.0063 258.7548
  10. # [[3]]
  11. # min max
  12. # 250.8689 267.8811
  13. # [[4]]
  14. # min max
  15. # 264.6437 283.3064
  16. # [[5]]
  17. # min max
  18. # 251.2302 264.3898
  19. # [[6]]
  20. # min max
  21. # 263.9332 284.9471
  22. # [[7]]
  23. # min max
  24. # 262.7223 284.9031
  25. # [[8]]
  26. # min max
  27. # 256.5893 274.8613
  28. # [[9]]
  29. # min max
  30. # 233.2658 249.6579
  31. # [[10]]
  32. # min max
  33. # 264.6657 284.1243
  34. # $gt
  35. # min max
  36. # 0 0

答案3

得分: 0

我们可以使用 tapply + col + range 来实现如下操作:

  1. > tapply(data, col(data), range)
  2. $`1`
  3. [1] 269.6438 284.8567
  4. $`2`
  5. [1] 242.0063 258.7548
  6. $`3`
  7. [1] 250.8689 267.8811
  8. $`4`
  9. [1] 264.6437 283.3064
  10. $`5`
  11. [1] 251.2302 264.3898
  12. $`6`
  13. [1] 263.9332 284.9471
  14. $`7`
  15. [1] 262.7223 284.9031
  16. $`8`
  17. [1] 256.5893 274.8613
  18. $`9`
  19. [1] 233.2658 249.6579
  20. $`10`
  21. [1] 264.6657 284.1243
  22. $`11`
  23. [1] 0 0
英文:

We can use tapply + col + range like below

  1. > tapply(data, col(data), range)
  2. $`1`
  3. [1] 269.6438 284.8567
  4. $`2`
  5. [1] 242.0063 258.7548
  6. $`3`
  7. [1] 250.8689 267.8811
  8. $`4`
  9. [1] 264.6437 283.3064
  10. $`5`
  11. [1] 251.2302 264.3898
  12. $`6`
  13. [1] 263.9332 284.9471
  14. $`7`
  15. [1] 262.7223 284.9031
  16. $`8`
  17. [1] 256.5893 274.8613
  18. $`9`
  19. [1] 233.2658 249.6579
  20. $`10`
  21. [1] 264.6657 284.1243
  22. $`11`
  23. [1] 0 0

huangapple
  • 本文由 发表于 2023年7月5日 00:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定