How to change array elements that are greater than 5 to 5, in one line?

Smithey

I would like to take an array x and change all numbers greater than 5 to 5. What is the standard way to do this in one line?

Below is some code that does this in several lines. This question on logical indexing is related but appears to concern selection rather than assignment. Thanks

x = [1 2 6 7]
for i in 1:length(x)
    if x[i] >= 5 
        x[i] = 5
    end
end

Desired output: x = [1 2 5 5]

Colin T Bowers

The broadcast operator . works with any function, including relational operators, and it also works with assignment. Hence an intuitive one-liner is:

x[x .> 5] .= 5

This part x .> 5 broadcasts > 5 over x, resulting in a vector of booleans indicating elements greater than 5. This part .= 5 broadcasts the assignment of 5 across all elements indicated by x[x .> 5].

However, inspired by the significant speed-up in Benoit's very cool answer below (please do check it out) I decided to also add an optimized variant with a speed test. The above approach, while very intuitive looking, is not optimal because it allocates a temporary array of booleans for the indices. A (more) optimal approach that avoids temporary allocation, and as a bonus will work for any predicate (conditional) function is:

function f_cond!(x::Vector{Int}, f::Function, val::Int)
    @inbounds for n in eachindex(x)
        f(x[n]) && (x[n] = val)
    end
    return x
end

So using this function we would write f_cond!(x, a->a>5, 5) which assigns 5 to any element for which the conditional (anonymous) function a->a>5 evaluates to true. Obviously this solution is not a neat one-liner, but check out the following speed tests:

julia> using BenchmarkTools

julia> x1 = rand(1:10, 100);

julia> x2 = copy(x1);

julia> @btime $x1[$x1 .> 5] .= 5;
  327.862 ns (8 allocations: 336 bytes)

julia> @btime f_cond!($x2, a->a>5, 5);
  15.067 ns (0 allocations: 0 bytes)

This is just ludicrously faster. Also, you can just replace Int with T<:Any. Given the speed-up, one might wonder if there is a function in Base that already does this. A one-liner is:

map!(a->a>5 ? 5 : a, x, x)

and while this significantly speeds up over the first approach, it falls well short of the second.

Incidentally, I felt certain this must be a duplicate to another StackOverflow question, but 5 minutes searching didn't reveal anything.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select array elements that are greater than 5% of a sum

From Dev

How to check that all elements of one array are greater than their counterparts in a parallel array (in Ruby).

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

How can I get the numbers/elements less than 5 of an array on swift code?

From Dev

Update if count is greater than 5 in MySQL

From Dev

Hide/Show if Count is Greater Than 5

From Dev

Select items where their amount is greater than 5

From Dev

How to find the elements greater than integer

From Dev

Is it possible to Multiply elements in the array if the elements value is greater than 10?

From Dev

Laravel 5+, gathering more than one of the same collection in an array

From Dev

How to change a string if the variable is greater than the current

From Dev

How to check if one value is greater than other

From Dev

How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

From Dev

How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

From Dev

How to remove all elements with values less than 5 in an object in JavaScript?

From Dev

How to count the elements in array which are more than one (php)

From Dev

how to verify difference time between linux machines not greater than 5 seconds

From Dev

How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

From Dev

Assign 0 and 1 based on values greater than in one line

From Dev

How to filter array values greater than x

From Java

Replace all elements of Python NumPy Array that are greater than some value

From Dev

Remove all elements from array greater than n

From Dev

Check if at least two elements in an array are greater than zero - JavaScript/Typescript

From Dev

Quick comparison of numpy array elements, greater or less than each other

From Dev

PostgreSQL: Replace values greater than a certain limit in array elements

From Dev

How to Add More than One Item to Middleware on Route in Laravel 5

From Dev

How to user input the array elements in c++ in one line

From Dev

Eclipse Does Not Recognize JRE 8 as Greater Than JRE 5

Related Related

  1. 1

    Select array elements that are greater than 5% of a sum

  2. 2

    How to check that all elements of one array are greater than their counterparts in a parallel array (in Ruby).

  3. 3

    How to find number of elements greater/smaller than an element in an array?

  4. 4

    How to find number of elements greater/smaller than an element in an array?

  5. 5

    How can I get the numbers/elements less than 5 of an array on swift code?

  6. 6

    Update if count is greater than 5 in MySQL

  7. 7

    Hide/Show if Count is Greater Than 5

  8. 8

    Select items where their amount is greater than 5

  9. 9

    How to find the elements greater than integer

  10. 10

    Is it possible to Multiply elements in the array if the elements value is greater than 10?

  11. 11

    Laravel 5+, gathering more than one of the same collection in an array

  12. 12

    How to change a string if the variable is greater than the current

  13. 13

    How to check if one value is greater than other

  14. 14

    How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

  15. 15

    How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

  16. 16

    How to remove all elements with values less than 5 in an object in JavaScript?

  17. 17

    How to count the elements in array which are more than one (php)

  18. 18

    how to verify difference time between linux machines not greater than 5 seconds

  19. 19

    How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

  20. 20

    Assign 0 and 1 based on values greater than in one line

  21. 21

    How to filter array values greater than x

  22. 22

    Replace all elements of Python NumPy Array that are greater than some value

  23. 23

    Remove all elements from array greater than n

  24. 24

    Check if at least two elements in an array are greater than zero - JavaScript/Typescript

  25. 25

    Quick comparison of numpy array elements, greater or less than each other

  26. 26

    PostgreSQL: Replace values greater than a certain limit in array elements

  27. 27

    How to Add More than One Item to Middleware on Route in Laravel 5

  28. 28

    How to user input the array elements in c++ in one line

  29. 29

    Eclipse Does Not Recognize JRE 8 as Greater Than JRE 5

HotTag

Archive