# Numbering indices / array element positions while creating an array (using 3 argument form)

I am new to Mathematica and trying to create a list that I can use within the InterpolatingPolynomial function. e.g. {{-1, 0},{ 0, 0},{1, 1}}.

I extended the "linearMesh" function from here: https://mathematica.stackexchange.com/a/32721 to insert n array elements

linearmesh[a_, b_, n_Integer] := Array[# &, n, {a, b}]


became:

linearmesh[a_, b_, n_Integer] :=
Array[# &,
n, {{a, KroneckerDelta[i, j]}, {b, KroneckerDelta[i, j]}}];


My aim is to produce, e.g. for a==-1 and b==1 and 3 elements:

{{-1, KroneckerDelta[i, j]}, {0, KroneckerDelta[i, j]}, {1,
KroneckerDelta[i, j]}}


where each of the KroneckerDeltas is calculated with respect to a variable i, and j is the index of the array element. How can I get the index, since the array is being defined at the same time?

What I tried:

linearmesh[a_, b_, n_Integer] :=
Array[# &,
n, {{a, KroneckerDelta[i, #1]}, {b, KroneckerDelta[i, #1]}}];


and then linearmesh[-1,1,3] gives me results:

{{-1, KroneckerDelta[i, #1]}, {0, KroneckerDelta[i, #1]}, {1, KroneckerDelta[i, #1]}}


rather than substituting the values of each of the # indices and obtaining

{{-1, KroneckerDelta[i, 1]}, {0, KroneckerDelta[i, 2]}, {1, KroneckerDelta[i, 3]}}


I also considered using Parts, but I can't do that since the array doesn't exist yet while linearmesh is being defined.

Thanks!

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Mar 3 '16 at 18:22
• Thanks Louis! I came over from StackOverflow so I am familiar with the system!! I appreciate it. – Cogicero Mar 3 '16 at 18:27

## 2 Answers

One possibility:

linearmesh[a_, b_, n_Integer] := {a + (b - a)/(n - 1) #, KroneckerDelta[i, # + 1]} & /@ Range[0, n - 1]


The reason that

linearmesh[a_, b_, n_Integer] :=
Array[# &,
n, {{a, KroneckerDelta[i, #1]}, {b, KroneckerDelta[i, #1]}}];


doesn't work is that the third argument to Array can't be a function (and certainly the expressions put in place of # in the first argument don't get also fed to the third argument).

• Thanks! This seems perfect, but it doesn't work for other values of n (except 3). The range is not being properly calculated. Could you please help check? e.g. for 2 items I have {{0, KroneckerDelta[1, i]}, {2, KroneckerDelta[2, i]}} instead of {{-1, KroneckerDelta[1, i]}, {1, KroneckerDelta[2, i]}} – Cogicero Mar 3 '16 at 18:25
• @Cogicero. Indexing problem. Edited. Does it work now? – march Mar 3 '16 at 18:27
• Thanks for the edit! I further edited it to linearmesh[a_, b_, n_Integer] := {a + (b - a)/(n - 1) #, KroneckerDelta[i, # + 1]} & /@ Range[0, n - 1] nd it works exactly as intended. Accepting your answer! Thanks again – Cogicero Mar 3 '16 at 18:30
• @Cogicero. Yes, missed that. I advise not accepting this just yet. Other people are likely to come up with more clever solutions, and the more answers the better. – march Mar 3 '16 at 18:31
• An alternative: linearmesh[a_, b_, n_Integer] := Transpose[{Subdivide[a, b, n - 1], Thread[KroneckerDelta[i, Range[n]]]}] – J. M. will be back soon Mar 3 '16 at 22:42

You can use MapIndexed.

linearmesh[a_, b_, n_Integer] :=
MapIndexed[{#1, KroneckerDelta[i, First@#2]} &]@Array[# &, n, {a, b}]


Then

linearmesh[-1, 1, 3]
(* {{-1, KroneckerDelta[1, i]}, {0, KroneckerDelta[2, i]}, {1, KroneckerDelta[3, i]}} *)


The order changes but KroneckerDelta is orderless. Actually, that is perhaps why it changes since it knows what the numbers are but not the i.

Hope this helps.

• Checking out MapIndexed. Thank you! – Cogicero Mar 3 '16 at 19:08