将减法结果限制为最小零 [英] Limit result of subtraction to a minimum of zero

查看:49
本文介绍了将减法结果限制为最小零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量'x',其值的范围是0到1,例如 x<-c(0,0.5.1).我要从"x"中减去0.5:

I have a vector 'x' where values ranges from 0 to 1, e.g. x <- c(0, 0.5. 1). I'm subtracting, say, 0.5 from 'x':

x - 0.5

x-0.5 的结果范围为-0.5至0.5.但是,我想将结果的最小值限制为0,即新范围将为0.5到0,任何以前为负的数字现在都将强制为0.

The result of x - 0.5 will range from -0.5 to 0.5. However, I want to constrain the minimum of the result to 0, i.e. the new range will be 0.5 to 0, any previously negative numbers will now be coerced to 0.

有没有简单的方法可以做到这一点?我一直在寻找约束"和限制"等等.我想我可以用 if 或过滤将其变形,但是我希望有一个优雅的功能没有出现在我的搜索中.

Is there a simple way of doing this? I've looked for "constrain" and "limit" and such. I assume I could probably bash it into shape with if or filtering but I was hoping there was an elegant function that hasn't surfaced in my searches.

推荐答案

请参见?pmax .

pmax(0, x - 0.5)

即选择较大的那个-如果 x<则为零.0.5 .

I.e. pick whichever is larger -- that would be zero if x < 0.5.

这篇关于将减法结果限制为最小零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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