Re: Inverse of IntegerDigits? and "Index" function?
- To: mathgroup at smc.vnet.net
- Subject: [mg3958] Re: Inverse of IntegerDigits? and "Index" function?
- From: espen.haslund at fys.uio.no (Espen Haslund)
- Date: Sat, 11 May 1996 23:53:31 -0400
- Organization: Universitet i Oslo
- Sender: owner-wri-mathgroup at wolfram.com
In article <4menl1$jva at dragonfly.wolfram.com>, pehowland at taz.dra.hmg.gb says...
;>
;>Excuse me if I'm being particularly inept this morning, but can anyone offer
;>any help on the following problems please?
;>
;>Inverse of IntegerDigits
;>========================
;>I'm playing around with some binary numbers, and am using IntegerDigits
;>to convert from a decimal integer to the binary notation. eg.
;> In[1]:= IntegerDigits[10,2]
;> Out[1]= {1,0,1,0}
;>
;>What is the function to convert back? eg. I want to do
;> In[2]:= DigitsToInteger[{1,0,1,0},2]
;>and get
;> Out[2]= 10
;>I've scoured the manual and can't find the function. This led me to
;>try to write my own function, which identified another problem...
;>
;>"Index" Function
;>================
;>Writing a function to convert back is easy in principle, eg.
;>
;> DigitsToInteger[x_,n_] := Apply[Plus[Map[(#*n^(Index[#]-1))&, Reverse[x]]]]
;>
;>would do the trick, but I seem to require a function, that I've called
;>Index[] here, that when Map[]'d across a list returns the position of
;>each element. Thus I want a function that works as follows:
;>
;> In[3]:= Map[Index[#]&, {2,6,3,9,10,2,4}]
;> Out[3]= {1,2,3,4,5,6,7}
;>
;>But I can't seem to find this function either!
;>
;>For the moment I'm using the following more Fortran-like function, which
;>works, but disturbs my sensibilities:
....
;>Any suggestions?
;>
Hi, Paul:
I think your Index function is impossible to implement.
I hope that the function below does what you want:
DigitsToInteger[x_,n_] := Plus @@ (Table[n^i,{i,Length[x],1,-1}]*x)
-Espen
==== [MESSAGE SEPARATOR] ====