Is there a way to do elementwise function argument?

For example I have a array [4,5,6,7] and I want to clamp the first element to 1, second element to 2, third element to 3 and fourth element to 4. Can I do something like clamp.([4,5,6,7],[0,0,0,0],[1,2,3,4]) and somehow Julia interprets it as [clamp(4,0,1),clamp(5,0,2),clamp(6,0,4),clamp(7,0,4)]?

Actually clamp.([4,5,6,7],[0,0,0,0],[1,2,3,4]) is the right syntax for that

3 Likes

You can also do clamp.([4,5,6,7],0,[1,2,3,4]) to take advantage of the lower bounds all being 0.

3 Likes

Or even

clamp.([4,5,6,7], 0, 1:4)
2 Likes

I thought the dot operator only works for 1 array inputs and the others are scalars, something like sin.([1,2,3,4]). Is there a tutorial/documentation on what the dot operator can or cannot do?

It’s actually in several places in the manual, here’s a start. The dot (not really an operator on its own) is syntactic sugar for broadcasting.

2 Likes