簡體   English   中英

根據它們是否高於或低於某個閾值來更改矩陣中的值

[英]Changing values in a matrix depending on whether they are above of below a certain threshold value

我有一個矩陣。 如果矩陣的某個元素大於 400,那么我希望該元素變為零。 如果矩陣的某個元素小於 400,那么我想將該元素乘以 3。

以下是如何重現我的矩陣:

structure(c(122, 948, 952, 100,
942, 150, 150, 149, 
244, 220, 437, 395, 
356, 473, 434, 335, 
357, 371, 590, 553, 
520, 491, 426, 426, 
427, 177, 284, 338, 
391, 290, 345, 399, 
143, 193, 136, 121, 
122, 187, 177, 544), .Dim = c(10L, 4L), units = structure(list(
    numerator = "m", denominator = character(0)), class = "symbolic_units"), class = "units")

使用Matrix作為您的數據:

for(i in 1:dim(Matrix)[2])
{
  Matrix[,i] <- ifelse(Matrix[,i]>400,0,
                       ifelse(Matrix[,i]<400,3*Matrix[,i],Matrix[,i]))
}

      [,1] [,2] [,3] [,4]
 [1,]  366    0    0 1035
 [2,]    0 1185    0 1197
 [3,]    0 1068    0  429
 [4,]  300    0    0  579
 [5,]    0    0    0  408
 [6,]  450 1005  531  363
 [7,]  450 1071  852  366
 [8,]  447 1113 1014  561
 [9,]  732    0 1173  531
[10,]  660    0  870    0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM