Home > Mobile >  Elementwise max in CartesianIndex in Julia
Elementwise max in CartesianIndex in Julia

Time:08-29

When I need to get element-wise max of two vectors a and b, all we need is

a = [2,4,5,4]
b = [5,2,8,1]
max.(a,b)
# [5,4,8,4]

What if a and b is CartesianIndex? The following simple code does not work.

a = CartesianIndex(2,4,5,4)
b = CartesianIndex(5,2,8,1)
max.(a,b)

In this example, I need CartesianIndex(5,4,8,4).

Then, I wrote the following code.

CartesianIndex(max.(a.I,b.I)...)

It works, but I am not sure if it is the correct way. If you have a better idea, I would like to know it.

CodePudding user response:

max on CartesianIndex values already works that way.

julia> a = CartesianIndex(2,4,5,4)
CartesianIndex(2, 4, 5, 4)

julia> b = CartesianIndex(5,2,8,1)
CartesianIndex(5, 2, 8, 1)

julia> max(a, b)
CartesianIndex(5, 4, 8, 4)
  • Related