# Can we get a standard optimised function to map an array's elements randomly with n elements of another array each?

**URL:** <https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594>\
**Category:** General Usage\
**Created:** [January 25, 2018, 12:32pm UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594 "2018-01-25T12:32:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [January 25, 2018, 12:32pm UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/1 "2018-01-25T12:32:37Z")

</div>

Can we an optimised standard function in the library for mapping/linking _w_ elements of an array with _n_ elements of another array each?  
Meaning a space’s elements should randomly get connected to another space with n links each? Or, maybe, further give parameter to introduce ordered links instead of the randomly chosen links?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 25, 2018, 1:09pm UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/2 "2018-01-25T13:09:38Z")

</div>

Please provide a minimal working example of at least the input and the output that you would expect.

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [January 26, 2018, 6:30am UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/3 "2018-01-26T06:30:58Z")

</div>

```julia
function rand1DLinks(layer1len::Int, layer2len::Int, N::Int)
    connectionsMap::AbstractArray = []
    for i = 1:layer2len
        push!(connectionsMap, rand(1:layer1len, N))
    end
    connectionsMap
end

```

_This is an example function for 1D arrays which are not mapped according to topographic map rules, just random connections._

Can we have an optimized standard function for linking **m** _x_ **n** array with **j** _x_ **k** array?  
The return type could a Dict() but an array format as in the above example seems more convenient. 🤔

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 26, 2018, 7:16am UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/4 "2018-01-26T07:16:33Z")

</div>

Sorry, but I don’t understand what the issue is. You ask for a “standard function in the library”, which library? You may want to open an issue for that library, but this hardly seems like something that would warrant a function (perhaps I am missing some context).

And it seems very straightforward to implement and is already fairly close to being “optimal”. You are essentially generating a bunch of random numbers in `1:layer1len`. You could make this faster by collecting into a matrix, which you preallocate, eg

```julia
connectionsMap = Matrix{Int}(layer2len, N) # reuse this
rand!(connectionsMap, 1:layer1len) # update when needed

```

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [January 26, 2018, 7:49am UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/5 "2018-01-26T07:49:04Z")

</div>

How will the same routine go about for **m** _x_ **n** matrix? Where instead of layer1len and layer2n you are given **m** _x_ **n** for both layers. So those are two Int values… Which won’t go in the Matrix argument as we still have N.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 26, 2018, 8:05am UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/6 "2018-01-26T08:05:09Z")

</div>

I would really like to help you, but I don’t understand the specs.

If you want help with optimizing something, please provide working code, eg with loops.

---

<div class="post-metadata">

**Author:** ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)\
**Post date:** [January 26, 2018, 9:05am UTC](https://discourse.julialang.org/t/can-we-get-a-standard-optimised-function-to-map-an-arrays-elements-randomly-with-n-elements-of-another-array-each/8594/7 "2018-01-26T09:05:28Z")

</div>

> [@Tamas\_Papp](#):
>
> ```julia
> connectionsMap = Matrix{Int}(layer2len, N) # reuse this
> rand!(connectionsMap, 1:layer1len) # update when needed
> 
> ```

It is similar to the function you gave except for arrays that are not limited to one dimension , like **m** _x_ **n** , so that every element of array **A** is connected to **N** elements of another array **B** , but in such a way that every element of array B has some connections and no element gets unfair connections.  
I searched online and it seems that **Self Organising Maps** come closest to this I guess.

I found some images that represent this idea, kind of:  
[Pic1](http://www.pitt.edu/~is2470pb/Spring05/FinalProjects/Group1a/tutorial/kohonen1.gif)  
[Pic2](http://fourier.eng.hmc.edu/e161/lectures/figures/SOM.gif)
