[Date Index]
[Thread Index]
[Author Index]
Re: Re: Using Select with arrays? (Relative newbie)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg54244] Re: Re: Using Select with arrays? (Relative newbie)
*From*: Hugo Mallinson <hfm21 at cam.ac.uk>
*Date*: Mon, 14 Feb 2005 21:50:36 -0500 (EST)
*References*: <cup5ip$dsr$1@smc.vnet.net> <200502140557.AAA17011@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Thanks very much! My typos didn't help matters (the strings were
actually strings, I just left out the quotes to simplify things..or so
I thought). This appears to be more efficient than what I eventually
came up with:
Select[data, #[[1]]==1 && #[[4]]=="string"] and then a separate Max[]
call.
Hugo
On Feb 14, 2005, at 5:57 AM, Curt Fischer wrote:
> Hugo Mallinson wrote:
>> The subject might not be entirely correct, but here is my problem:
>>
>> I have a list of 5-variable data points like
>>
>> data = {
>> {1, 1, string1, c, d}
>> {1, 2, string2, c, d}
>> {1, 3, string3, c, d}
>> {1, 4, string4, c, d}
>> {2, 1, string1, c, d}
>> {2, 2, string2, c, d}
>> {3, 1, string3, c, d}
>> {4, 1, string2, c, d}
>> {4, 2, string4, c, d}
>> }
>>
>>
>> and I want to extract just the points that have 1 (or 2, etc) as their
>> first value. I think I should do something like
>>
>> Select[data, {1, _Integer, _String, _Integer, _Integer}]
>>
>> but that doesn't work.
>
> None of your data matches the pattern you made. When you enter things
> like string2 or c or d into Mathematica without quotation marks, they
> are not strings or integers. You can easily find out what Mathematica
> thinks they are.
>
> In[1]:=
> data = {{1, 1,
> string1, c, d} , {1, 2, string2, c, d}, {1, 3,
> string3, c, d}, {1, 4, string4, c, d} , {2, 1,
> string1, c, d}, {2, 2, string2, c, d}, {3, 1,
> string3, c, d} , {4, 1, string2, c, d}, {4, 2, string4, c, d}};
>
> In[2]:=
> Map[Head, data, {2}]
> Out[2]=
> {{Integer, Integer, Symbol, Symbol, Symbol}, {Integer, Integer, Symbol,
> Symbol, Symbol}, {
> Integer, Integer, Symbol, Symbol, Symbol}, {Integer, Integer,
> Symbol,
> Symbol, Symbol}, {
> Integer, Integer, Symbol, Symbol, Symbol}, {Integer, Integer,
> Symbol,
> Symbol, Symbol}, {
> Integer, Integer, Symbol, Symbol, Symbol}, {Integer, Integer,
> Symbol,
> Symbol, Symbol}, {Integer, Integer, Symbol, Symbol, Symbol}}
>
> That is, they are all Symbols. We can re-write your pattern to be more
> flexible and we can use Cases[] instead of Select[]. (IME Cases[] is
> easier to figure out.)
>
> In[3]:=
> Cases[data, {1, _Integer, _, _, _}]
> Out[3]=
> {{1, 1, string1, c, d}, {1, 2, string2, c, d}, {
> 1, 3, string3, c, d}, {1, 4, string4, c, d}}
>
> So that gets you the preliminary result. But you want to extract the
> member of this list that has the maximal second element.
>
>> Having done that I need to find the maximum value of #2 for each
>> string, which I presumably do by the same method as above to extract
>> all string1 (or ...2) and then use Map[] and Max[]. I would do this
>> all
>> with For loops (revealing my lack of Mathematica chops :-) ) but I'd
>> really like to learn how to do this sort of extraction in a
>> Mathematica-y way. Any help would be greatly appreciated!
>
> Let's say you want to know the maximum integer that occurs in slot #2
> for all of the data that contain string3.
>
> In[4]:=
> Max[Part[#,2]&/@Cases[data,{_Integer,_Integer, string3,_,_}]]
>
> Out[4]=
> 3
>
> This code extracts the second part from all of the list elements that
> match {_Integer, _Integer, string3, _,_} and finds the maximum value
> thereof.
> --
> Curt Fischer
>
Prev by Date:
**Re: Problem using Evaluate[] inside a Plot command. 5.1**
Next by Date:
**Re: finding out what Automatic was**
Previous by thread:
**Re: Using Select with arrays? (Relative newbie)**
Next by thread:
**Re: Re: Re: Using Select with arrays? (Relative newbie)**
| |