MathGroup Archive 2009

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

Search the Archive

Re: Using Map or Thread over selected parts of nested lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95911] Re: Using Map or Thread over selected parts of nested lists
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 29 Jan 2009 05:59:17 -0500 (EST)

On 1/28/09 at 6:29 AM, paul.ellsmore at nanion.co.uk (Paul Ellsmore)
wrote:

>1)      My GUI allows the user to select some data files to work
>with. I use the code:

>source = SystemDialogInput[FileOpen]

>This returns the full path for each file selected by the user - very
>convenient and user-friendly. My problem is that "source" is
>returned as a string (ie an atom) if one file is selected, but a
>nested list if multiple files are selected. I have similar instances
>in many parts of my program where the user might be working with one
>list of data or multiple lists, and I think the same issue will
>arise. So I would like to know if there is a way of forcing (in this
>case) "source" to be returned as a nestedlist type, even if the
>length of that list is one?

A simple way to get the same structure in both cases would be to
use pattern matching as follows:

source = SystemDialogInput[FileOpen]/.a_String->{a}

>2)      I have data in the format:

>List1={{header,header},{header,header},{header,header},{frequency1,
>complexnumber1},{frequency2, complexnumber2},{frequency3,
>complexnumber4}.}

>On very many occasions I want to deal with :

>List2={{frequency1, realpart1},{frequency2, realpart2},{frequency3,
>realpart4}.}

>Or some other trivial transform of complexnumbern. I have been
>through the VirtualBook, and Documentation centre, and tried various
>forms of Map and Thread, but I cannot find a way of "mapping" or
>"threading" a function like Re[expr] over one element of part of a
>list, as in:

>List2={{frequency1, Re[complexnumber1]},{frequency2,
>Re[complexnumber2]},{frequency3, Re[complexnumber4]}.}

There are a couple of ways to achieve what you want here. First,
using Map with a pure function you could do:

list2= {First@#, Re@Last@#}&/@list2

Note, this does not deal with the header portion of list1 as written

another way would be to use pattern matching by doing:

list2 = list1/.{a_,b_Complex}->{a,Re[b]}

assuming the header entries do not have a complex number in the
second position, this should work with the header entries
included. In this case, the header entries get copied over to
list2 since they won't match the test pattern.

=46inally, without either pattern matching or using Map the same
result can be obtained with

list2 = Transpose@{list1[[All,1]],Re@list1[[All,2]]}



  • Prev by Date: Re: two y-axes with different scaling
  • Next by Date: Re: two y-axes with different scaling
  • Previous by thread: Using Map or Thread over selected parts of nested lists
  • Next by thread: Re: Using Map or Thread over selected parts of nested lists