Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:56:19+00:00 2026-06-15T20:56:19+00:00

In Mathematica there is the command Clip[x, {min, max}] which gives x for min<=x<=max

  • 0

In Mathematica there is the command Clip[x, {min, max}]
which gives x for min<=x<=max, min for x<min and and max for x>max, see

http://reference.wolfram.com/mathematica/ref/Clip.html (mirror)

What would be the fastest way to achieve this in R? Ideally it should be a function that is listable, and should ideally work on either a single value, vector, matrix or dataframe…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:56:20+00:00Added an answer on June 15, 2026 at 8:56 pm

    Rcpp has clamp for this:

    cppFunction('NumericVector rcpp_clip( NumericVector x, double a, double b){
        return clamp( a, x, b ) ;
    }')
    

    Here is a quick benchmark showing how it performs against other methods discussed :

    pmin_pmax_clip <- function(x, a, b) pmax(a, pmin(x, b) )
    ifelse_clip <- function(x, a, b) {
      ifelse(x <= a,  a, ifelse(x >= b, b, x))
    }
    operations_clip <- function(x, a, b) {
      a + (x-a > 0)*(x-a) - (x-b > 0)*(x-b)
    }
    x <- rnorm( 10000 )
    require(microbenchmark)
    
    microbenchmark( 
      pmin_pmax_clip( x, -2, 2 ), 
      rcpp_clip( x, -2, 2 ), 
      ifelse_clip( x, -2, 2 ), 
      operations_clip( x, -2, 2 )
    )
    # Unit: microseconds
    #                        expr      min        lq   median        uq       max
    # 1     ifelse_clip(x, -2, 2) 2809.211 3812.7350 3911.461 4481.0790 43244.543
    # 2 operations_clip(x, -2, 2)  228.282  248.2500  266.605 1120.8855 40703.937
    # 3  pmin_pmax_clip(x, -2, 2)  260.630  284.0985  308.426  336.9280  1353.721
    # 4       rcpp_clip(x, -2, 2)   65.413   70.7120   84.568   92.2875  1097.039    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to extract data from specific HTML tables using Mathematica?
My son demonstrated Mathematica to me a while back. Is there anything similar which
I'm wondering if there is a different command than FullSimplify to tell mathematica to
Is there a way in Mathematica to check whether a plot is empty or
I wonder if there exists way to work with large files in Mathematica ?
In Mathematica the command Select only lets define a selection criterion for each element
Consider the following dialog with the command-line interface to the kernel: $ math Mathematica
Is there a Mathematica function like inject in Ruby? For example, if I want
I often wish to see the internal representation of Mathematica 's graphical objects not
In Mathematica there are a number of functions that return not only the final

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.