# Mapping a value from one range to another

**URL:** <https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 7, 2022, 2:36am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423 "2022-08-07T02:36:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![GameDungeon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gamedungeon/32/38609_2.png) [@GameDungeon](https://discourse.julialang.org/u/GameDungeon)\
**Post date:** [August 7, 2022, 2:36am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/1 "2022-08-07T02:36:11Z")

</div>

I have two ranges a and b.

```julia
a = 1:10
b = 1:100

```

I have a value from a, say 5. If I were to map it onto range b its equivalent would be 50. In python you can write a function that does this like this:

```julia
def map_range(range1: tuple, range2: tuple, val):
    slope = (range2[1] - range2[0]) / (range1[1] - range1[0])
    return range2[0] + slope * (val - range1[0])

```

With the more unique way julia handles ranges I was wondering if it has a better method of doing something like this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 7, 2022, 3:04am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/2 "2022-08-07T03:04:07Z")

</div>

you can decide what the edge case behavior you want, but this should do what you want in case of divisible ranges

```julia

julia> function map_range(range1, range2, val)
           slope = length(range2)÷length(range1)
           return range2[findfirst(==(val), range1) * slope]
       end
map_range (generic function with 1 method)

julia> map_range(1:10, 1:100, 6)
60

julia> map_range(1:10, 1:100, 5)
50

julia> map_range(1:20, 1:100, 5)
25

julia> map_range(1:20, 1:100, 10)
50

```

---

<div class="post-metadata">

**Author:** ![GameDungeon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gamedungeon/32/38609_2.png) [@GameDungeon](https://discourse.julialang.org/u/GameDungeon)\
**Post date:** [August 7, 2022, 3:05am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/3 "2022-08-07T03:05:42Z")

</div>

What do you mean by divisible ranges? Any two ranges that can be divided, or like divided evenly?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 7, 2022, 3:12am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/4 "2022-08-07T03:12:25Z")

</div>

your python function would map val into values that didn’t exist in the second range, `a:b` in Julia by default has unit step

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2022, 4:56am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/5 "2022-08-07T04:56:29Z")

</div>

> [@GameDungeon](#):
>
> `slope = (range2[1] - range2[0]) / (range1[1] - range1[0])`

This one’s really confusing. For those two ranges `slope` would be equal to `1.0`. So 5 would map to 5.0. But you say it maps to 50?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 7, 2022, 5:02am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/6 "2022-08-07T05:02:06Z")

</div>

in python they pass (start, end) tuple as “range”

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2022, 5:31am UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/7 "2022-08-07T05:31:54Z")

</div>

I thought they used `range(start, stop)` or something.

I guess I was confused by the use of the word ‘range’, and the reference to `1:10`. The OP actually isn’t asking about ranges at all, but tuples, both in Python and Julia.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 7, 2022, 1:22pm UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/8 "2022-08-07T13:22:47Z")

</div>

> [@GameDungeon](#):
>
> With the more unique way julia handles ranges I was wondering if it has a better method of doing something like this?

```julia
julia> function map_ranges(t1 ::Tuple, t2::Tuple, val)
           r1=range(t1...)
           r2=range(t2...,length(r1))
           return r2[val]
       end
map_ranges (generic function with 2 methods)

julia> map_ranges((1,10),(2,37),1)
2.0

julia> map_ranges((1,10),(2,37),10)
37.0

julia> map_ranges((1,10),(2,37),7)
25.333333333333332

```

This scheme seems to me more adherent to the Python version and responds to the expectation that the same mapping can be done in Julia in a more “simple” way

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 7, 2022, 2:29pm UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/10 "2022-08-07T14:29:02Z")

</div>

In my opinion, it’s as you say! As I wrote without using the linear transformation formula, which is implicitly realized by the length parameter of `range(start, stop, length)`.  
Being a linear mapping, it needs to be verified (only) in two distinct points (only one straight line passes through two points 😄).

```julia

1--> 1
10--> 100

```

---

<div class="post-metadata">

**Author:** ![GameDungeon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gamedungeon/32/38609_2.png) [@GameDungeon](https://discourse.julialang.org/u/GameDungeon)\
**Post date:** [August 7, 2022, 4:30pm UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/11 "2022-08-07T16:30:37Z")

</div>

The reason you get 45 is that in python the same range would be 0 to 100 (not including the end). Yes, the python version uses tuples because using actual ranges in python would be impractical. I was wondering if there was a better way to do it in julia. Using actual ranges is more readable imo.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2022, 5:25pm UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/13 "2022-08-07T17:25:16Z")

</div>

> [@GameDungeon](#):
>
> using actual ranges in python would be impractical. I was wondering if there was a better way to do it in julia. Using actual ranges is more readable imo.

To me it looks like ranges are the wrong concept for this. Aren’t you actually looking for an _interval_. Ranges don’t just have a start and a stop, but a steplength or resolution as well. It doesn’t make sense to me to say that `1:10` vs `1:100` maps 5 to 50. This makes more sense for _intervals_ (though it should be [0, 10] and [0, 100]).

Tuples are more suited to describing intervals than ranges are.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 7, 2022, 5:34pm UTC](https://discourse.julialang.org/t/mapping-a-value-from-one-range-to-another/85423/14 "2022-08-07T17:34:46Z")

</div>

It is not clear to me if this mapping is what you are looking for.

```julia

julia> map_range(1:10, 1:50, 1)
5

julia> map_range(1:10, 1:100, 1)
10

julia> map_range(0:10, 0:50, 1)
7

```
