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

#1

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?

#2

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

#3
``````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.

#4

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

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

#5

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.

#6

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.

#7

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
Pic2