Re: slots with 2 indexed array?
- To: mathgroup at smc.vnet.net
- Subject: [mg95612] Re: slots with 2 indexed array?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 22 Jan 2009 07:04:57 -0500 (EST)
On 1/21/09 at 6:47 AM, wb at wavebounce.com wrote:
>First I produce some dummy data, 4000 of what I call signals, 1024
>samples each.
>sigs = Table[10 i + j, {i, 4000}, {j, 1024}]; len = Length[sigs];
>I'd like to modify these signals. The For statement below works. I
>can use a slot function on each of the sigs specified by i.
>For[i = 1, i < len + 1, i++, sigs[[i]] = If[# >= 2048, # -
>2048, # + 2048] & /@ sigs[[i]] ]
While the loop above clearly works, it is a very inefficient way
to get the desired result. It is more efficient to do
sigs = Map[If[# >= 2048, # - 2048, # + 2048] &, sigs, {2}];
That is:
In[1]:= test = sigs = Table[10 i + j, {i, 4000}, {j, 1024}]; len =
Length[sigs];
In[2]:=
=46or[i = 1, i < len + 1, i++,
sigs[[i]] =
If[# >= 2048, # - 2048, # + 2048] & /@ sigs[[i]]] // Timing
Out[2]= {1.35373,Null}
In[3]:=
test = Map[If[# >= 2048, # - 2048, # + 2048] &, test, {2}]; // Timing
Out[3]= {0.859782,Null}
In[4]:= test == sigs
Out[4]= True
>Surely there's a way to use slots on a double indexed array but I
>haven't found it yet.
>If I try to let # represent every entry in sigs, the If statement
>isn't processed:
>sigs = If[# >= 2048, # - 2048, # + 2048] & /@ sigs;
Of course the reason this doesn't work is the slot is replaced
by an entire row in sigs rather than an individual element. You
can make the if function map to individual elements by using the
third argument to Map as I did above.