0
$\begingroup$

I have the following Mathematica code where I create a linearly spaced array of numbers and try to retrieve the third element:

tN = 48;
linearmesh[a_, b_, n_Integer /; n > 1] := Range[a, b, (b - a)/(n - 1)]
a = linearmesh[0, 2*Pi, tN + 1] // N // Short
a[[3]]

This code results in the following output:

{0.,0.1309,0.261799,<<43>>,6.02139,6.15229,6.28319}
Part::partw: Part 3 of {0.,0.1309,0.261799,<<43>>,6.02139,6.15229,6.28319} does not exist.

I don't understand why this doesn't give me the third element of the array but instead gives me an error message?

$\endgroup$
2
  • $\begingroup$ It is nice to link sources of the code: mathematica.stackexchange.com/q/32715/5478 $\endgroup$
    – Kuba
    Commented Apr 19, 2017 at 7:34
  • $\begingroup$ I've made it a duplicate of a closely related topic (which answer is the answer to your problem) to prevent it from being closed as a simple mistake. Let me know if you disagree. $\endgroup$
    – Kuba
    Commented Apr 19, 2017 at 7:38

2 Answers 2

3
$\begingroup$

Look at Head[a]. It will be Short. It's a precedence issue between // and =.

Never do this: var = value // someForm because it will assign someForm[value] to var.

This is fine:

arr = longCalculation[]; // AbsoluteTiming

because the ; separates the assignment from AbsoluteTiming

$\endgroup$
1
$\begingroup$

Just remove the "Short" command, which generates a text string

    a = linearmesh[0, 2*Pi, tN + 1] // N

    a//Length
    49

If you really want the Short[ ] output then I use this pattern...

    (a = linearmesh[0, 2*Pi, tN + 1] // N) //Short
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.