Perhaps something like this is what you are looking for?
Suppose you have an array of objects. I have used the Array
function here, but the contents of this array could be anything.
elements = Array[a, {10}]
(*{a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10]}*)
Create a separate vector of energies. Since István has already shown the NormalDistribution
, I'll use ExponentialDistribution
.
energies = RandomVariate[ExponentialDistribution[1.5], {10}]
(*{0.0969302, 0.335765, 0.564123, 1.51769, 0.142579, 0.222407, \
0.842437, 1.35341, 0.239727, 0.0312016}*)
Now consider a function like this, which takes the index of the element and returns both the element and the associated energy. Notice that I have specified that n
has to be a positive integer, because indices of lists are always positive integers, and that it must also be less than the total length of the elements list.
getElement[n_Integer?Positive] /;
n <= Length[elements] := {elements[[n]], energies[[n]]}
getElement[4]
(*{a[4], 1.51769}*)
Because 20 is greater than the current length of elements
, getElement[20]
is undefined and returns itself unchanged.
getElement[20]
(* getElement[20] *)
But if you go back and redefine elements
(and energies
) to be longer, then re-evaluating getElement[20]
will return the 20th element-energy pair.
You could of course include some other checks to make sure that energies
is always the same length as elements
, but this should be enough to get you started.
EDIT
Based on your edit, it is possible that this is more appropriate. This function works out the position of the element in a particular vector, and returns that element and the matching element from the energies
vector.
getEnergyByElement[elem_, vec_?VectorQ] :=
With[{n = Position[vec, elem][[1, 1]]}, {vec[[n]], energies[[n]]}]
getEnergyByElement[a[4], elements]
(* {a[4], 1.51769} *)
An even simpler way is
{#, First@Pick[energies, elements, #]} &@a[5]
(*{a[5], 0.142579} *)
For
loop.Xarray=A @@@ Tuples[Range[0, 9], 3]
will do just as well, and it's much, much faster to type and to evaluate. $\endgroup$