Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting a list element into a number?

1,710 views
Skip to first unread message

meitnik

unread,
Apr 13, 2009, 3:35:28 AM4/13/09
to
Hi
I have noticed when using Position[], it returns a single element
list. So how do I convert that into a number other than using Flatten
to strip off the braces? It seems if I use Flatten within any
subroutine code, I get the error code thats its "Protected". So how do
I work around using Flatten?? Thanks

Bill Rowe

unread,
Apr 14, 2009, 6:15:02 AM4/14/09
to

Here is an example to get a number

In[2]:= Position[Range[5], 3][[1, 1]]

Out[2]= 3

to get a 1-D list

In[3]:= Position[Range[5], 3][[1]]

Out[3]= {3}

or

In[4]:= First@Position[Range[5], 3]

Out[4]= {3}

Note, there is no problem using Flatten in a subroutine. Getting
the error you described says there is something wrong with your
code. Possibly, you are making an assignment to Flatten which
would generate this error.

David Park

unread,
Apr 14, 2009, 6:18:42 AM4/14/09
to
There may be more than one position in an expression that matches a pattern.
And each position may have a multiple level specification. That is why the
result is returned as a list of lists. If there is only one position at the
first level you could use Part to extract its value.

pos = Position[x + y, y]
Part[pos, 1, 1]
{{2}}
2

Or you could write:

pos = Position[x + y, y]
pos[[1, 1]]
{{2}}
2

I often use the first form, just to be more explicit and get rid of double
brackets.

Another good practice in learning, and also in developing code and routines,
is to write multiple steps in one cell. Put each step on a new line (but in
the same cell). You can use % and %% to refer to the output of previous
lines. Then, when you evaluate you can see what each step is doing. Keep
changing, adding and experimenting, each time reevaluating the cell, until
you understand what is happening and you get what you want. If you wish, you
can even interlace the statements with Print statements to annotate what you
think you are doing.

Print["The initial expression"]
expr = x + y
Print["Find the position of y"]
Position[expr, y]
Print["Extract the number using Part"]
Part[%%, 1, 1]
Print["Check it using Part with the position on the expression"]
Part[expr, %%]

You could have multiple statements in a row, some of them with semicolons at
the end if you want to suppress that particular output. With or without the
Print statements this is a really useful way to develop a calculation.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/


From: meitnik [mailto:mei...@gmail.com]


Hi
I have noticed when using Position[], it returns a single element
list. So how do I convert that into a number other than using Flatten
to strip off the braces? It seems if I use Flatten within any
subroutine code, I get the error code thats its "Protected". So how do

I work around using Flatten?? Thanks

0 new messages