MathGroup Archive 2010

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

Search the Archive

Re: How do I take every 3rd number from a text file?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114289] Re: How do I take every 3rd number from a text file?
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Tue, 30 Nov 2010 04:03:13 -0500 (EST)

On Sun, 28 Nov 2010, truxton spangler wrote:

> On Nov 27, 7:37 pm, cubsfan334 <cubsfan... at gmail.com> wrote:
>> Hi,
>>
>> I've been reading in text files using the following command:
>>
>> ReadList["file.txt", Word]
>>
>> However, now I only want to read in every 3rd number, starting with
>> the valueI specify.
>>
>> For example, say I had these values:
>>
>> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
>>
>> How would I read in every third number, starting with the second
>> integer?  So the result would look like this:
>>
>> 1, 4, 7
>
> Assuming that the line above is actually a string: "0, 1, 2, 3, 4, 5,
> 6, 7, 8, 9" then use StringSplit and Take or Span, e.g.
>
> tmp = ToExpression@StringSplit["0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ", ", "]
>
> then
>
> Take[tmp, {2, Length[tmp], 3}]
>
> or
>
> tmp[[2 ;; Length[tmp] ;; 3]]
>
>

A small side note:

you can also index from the back with negative numbers


e.g. tmp[[ 2;; -1 ;; 3 ]]

Take[tmp, {2, -1, 3}]

Oliver


  • Prev by Date: Re: Compile can't handle indexed variables inside loops
  • Next by Date: Re: how to recover a corrupted notebook?
  • Previous by thread: Re: How do I take every 3rd number from a text file?
  • Next by thread: How to get coordinates needed to include all of graphics?