Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Please post: Mathematica -another For loop problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65768] Re: Please post: Mathematica -another For loop problem
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Mon, 17 Apr 2006 02:28:05 -0400 (EDT)
  • References: <e1suc9$c7k$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

1.156 wrote:
> [This post has been delayed due to email problems - moderator]
>
> I've got to manipulate some signed 12bit integer numbers from a file and
> apparently Mathematica only handles 8 and 16 bit signed numbers
> (probably for a very good reason). So I bring them in as 16 bits (and
> they're stored that way with a leading zero nibble) and then I have to
> do the following to make sense of them.  I really don't know why this
> code works but it does.
>
> For[i = 1, i < Length[y], i++, If[y[[i]] > 2048, y[[i]] =
>      y[[i]] - 2048, y[[i]] = y[[i]] + 2048]];
>
> So to my question: once again I had to resort to a For loop to pull this
> off and I know this is a no-no.  But I'm so clueless about Mathematica
> that I've never come up with a more sophisticated method after a year or
> so of thinking about it. Can someone help (as they usually can)?
>
> Thanks, Rob

hex  from    to
000     0     0
001     0     1
  :     :     :
7ff  2047  2047
800  2048 -2048
801  2049 -2047
  :     :     :
ffe  4094    -2
fff  4095    -1

z = Map[If[# < 2048, #, #-4096]&, y]

will do what you want. Note that Map returns a whole new vector that
can be called whatever you want. y is not automatically overwritten.
The equivalent short form is

z = If[# < 2048, #, #-4096]& /@ y


  • Prev by Date: Re: Please post: Mathematica -another For loop problem
  • Next by Date: Re: Accessing Mathematica on a remote server from java
  • Previous by thread: Re: Please post: Mathematica -another For loop problem
  • Next by thread: Mathlink and C/C++